﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Ports;
// DllImport
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.Visionox_Gamma
{
    public partial class R6645x_Gamma : Form
    {
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();

        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过

        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        private double lum_off, x_off, y_off;
        private int index = 0;
        private int flag1 = 1; //lum
        private int flag2 = 1; //x
        private int flag3 = 1; //y
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        private static decimal[] Last_reg_red = new decimal[100];
        private static decimal[] Last_reg_green = new decimal[100];
        private static decimal[] Last_reg_blue = new decimal[100];

        private static bool Gamma_adjust_timeout_flag = false;



        //---------------------R66455 the dll function define start-----------------------------------
        //===== Input =====
        private string productName;                                     // product selection
        private int[] Target_Level_NonLC_8b = new int[256];             // Target Level Non Linear Complete(8bit)
        private int[] Target_Level_NonLC = new int[1024];               // Target Level Non Linear Complete(10bit)
        private double[] Target_Voltage_R_NonLC = new double[1024];     // Target of Voltage R(pos) Non Linear Complete
        private double[] Target_Voltage_G_NonLC = new double[1024];     // Target of Voltage G(pos) Non Linear Complete
        private double[] Target_Voltage_B_NonLC = new double[1024];     // Target of Voltage B(pos) Non Linear Complete
        private double[] Measured_VPLVL_VNLVL = new double[2];          // V0/V1023 Voltage, [0]: V0, [1]: V1023
        private int[] Algorithm_Option = new int[2];		// Option for algorithm

        private double[] Algorithm_Setting = new double[22]; 			// Setting for algorithm

        private int[] Measured_Registers = new int[79];                // This setting will be ignored in OLED products
                                                                       //===== Output =====
        private int[] Adjusted_Registers = new int[79];                // Register settings after djustment

        private int Adjusted_REV;
        // Mapping for register
        private int[] Adjusted_X_R_CB = new int[13];
        private int[] Adjusted_Y_R_CB = new int[13];
        private int[] Adjusted_X_G_CB = new int[13];
        private int[] Adjusted_Y_G_CB = new int[13];
        private int[] Adjusted_X_B_CB = new int[13];
        private int[] Adjusted_Y_B_CB = new int[13];
        private int[] Adjusted_X_R_CC = new int[13];
        private int[] Adjusted_Y_R_CC = new int[13];
        private int[] Adjusted_X_G_CC = new int[13];
        private int[] Adjusted_Y_G_CC = new int[13];
        private int[] Adjusted_X_B_CC = new int[13];
        private int[] Adjusted_Y_B_CC = new int[13];

        private double[] R66455_CA310_LUM = new double[31];
        private double[] R66455_CA310_X = new double[31];
        private double[] R66455_CA310_Y = new double[31];
        private int[] R66455_GAMMA_GRAY = new int[31];


        /// <summary>
        /// 导入模拟键盘的方法
        /// </summary>
        /// <param name="bVk" >按键的虚拟键值</param>
        /// <param name= "bScan" >扫描码，一般不用设置，用0代替就行</param>
        /// <param name= "dwFlags" >选项标志：0：表示按下，2：表示松开</param>
        /// <param name= "dwExtraInfo">一般设置为0</param>
        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        // C# IntPtr <-->  C++ *
        [DllImport(@"Resources\GammaDll_PUB.dll", CallingConvention = CallingConvention.StdCall)]
        private extern static void Initialize_Product(
        );

        [DllImport(@"Resources\GammaDll_PUB.dll", CallingConvention = CallingConvention.StdCall)]
        private extern static void Adjust2DXYParam_Voltage(
            //===== Input =====
            [MarshalAs(UnmanagedType.LPStr)] string productName,                                    // product selection
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 1024)] int[] Target_Level_NonLC,          // Target Level Non Linear Complete
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 1024)] double[] Target_Voltage_R_NonLC,   // Target of Voltage R(pos) Non Linear Complete
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 1024)] double[] Target_Voltage_G_NonLC,   // Target of Voltage G(pos) Non Linear Complete
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 1024)] double[] Target_Voltage_B_NonLC,   // Target of Voltage B(pos) Non Linear Complete
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 2)] double[] Measured_VPLVL_VNLVL,        // V0/V1023 Voltage, [0]: V0, [1]: V1023
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 2)] int[] Algorithm_Option,   // Target of Voltage B(pos) Non Linear Complete
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 22)] double[] Algorithm_Setting,                                                                                                     //===== Output =====
            [Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 79)] int[] Adjusted_Registers         // Register settings after djustment
        );

        [DllImport(@"Resources\GammaDll_PUB.dll", CallingConvention = CallingConvention.StdCall)]
        private extern static void Adjust1p5DXYParam_Voltage(
            //===== Input =====
            [MarshalAs(UnmanagedType.LPStr)] string productName,                                    // product selection
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 1024)] int[] Target_Level_NonLC,          // Target Level Non Linear Complete
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 1024)] double[] Target_Voltage_R_NonLC,   // Target of Voltage R(pos) Non Linear Complete
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 1024)] double[] Target_Voltage_G_NonLC,   // Target of Voltage G(pos) Non Linear Complete
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 1024)] double[] Target_Voltage_B_NonLC,   // Target of Voltage B(pos) Non Linear Complete
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 2)] double[] Measured_VPLVL_VNLVL,        // V0/V1023 Voltage, [0]: V0, [1]: V1023
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 79)] int[] Measured_Registers,           // This setting will be ignored in OLED products
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 2)] int[] Algorithm_Option,               // Option for algorithm
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 22)] double[] Algorithm_Setting,
            //===== Output =====
            [Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 79)] int[] Adjusted_Registers       // Register settings after djustment
        );

        // C# IntPtr <-->  C++ *
        //[DllImport(@"Resources\\IRDrop.dll", CallingConvention = CallingConvention.StdCall)]
        static private double[,] MeasDataOf9Pos = new double[9, 3]; //Measurement data of 9 positions. 
        static private double[,] MeasDataOfGray = new double[7, 68]; //Measurement data of WRGBCMY Gray.
        static private int[] FlexGammaSetting = new int[79];                      //Flexible Gamma Setting.
                                                                                  // (e.g. REV, 0, R_CP00Y, R_CP01X, R_CP01Y, ...)      
        [DllImport(@"Resources\IRDrop.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        private extern static int IRDropAdjust(
            //===== Output =====
            ref int NumOfRegisters,                                                         // Number of Register settings after adjustment
            [Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 108)] int[] Registers,         // Register settings after adjustment
                                                                                              //===== Input =====
            [MarshalAs(UnmanagedType.TBStr)] string ProductID,                              // product ID
            [MarshalAs(UnmanagedType.I4)] int xsize,                                        // xsize
            [MarshalAs(UnmanagedType.I4)] int ysize,                                        // ysize
            [MarshalAs(UnmanagedType.I4)] int DBV,                                          // DBV
            [MarshalAs(UnmanagedType.I4)] int SPRtype,                                      // SPRtype
            [MarshalAs(UnmanagedType.I4)] int ErrorType,                                    // ErrorType
            [MarshalAs(UnmanagedType.R8)] double gamma,                                     // gamma
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 79)] int[] FlexGammaSetting,        // FlexGammaSetting
            [MarshalAs(UnmanagedType.LPArray, SizeConst = (9 * 3))] double[,] MeasDataOf9Pos,   // Measurement Data of 9 position (9*3)
            [MarshalAs(UnmanagedType.LPArray, SizeConst = (7 * 17 * 4))] double[,] MeasDataOfGray,// Measurement Data of Grayscale
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 4)] int[] others                    // other
       );
        //-------------------------------R66455 the dll function define end-----------------------------------------------------------------------------------------

        //R6645x 监听挂载
        frmMain frmHelp = null;
        public R6645x_Gamma(frmMain R6645x)
        {
            frmHelp = R6645x;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体   
        }
        //R6645x 监听卸载
        private void R6645x_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(R6645x_DataReceived);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "R6645x_Gamma_FormClosed！\r\n";
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.Child_window_hide_already = false;
            frmHelp.IC_comboBox.Enabled = true;
        }

        private void R6645x_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(R6645x_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "R6645x_Gamma_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(R6645x_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "R6645x_Gamma_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }
        private void R6645x_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    // 0x30: R66455 的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if (buffer[0] == 0x30 && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }

                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmHelp.T1.Enabled = false; //停止超时计数
                        if (Rec_tmp[0] == 0x30)  //R66455
                        {
                            switch (Rec_tmp[1] & 0x0f)
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "寄存器\t写 OK！\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        string tmp = "";
                                        for (int i = 3; i <= (Rec_tmp[2] + 2); i++)
                                        {
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";//16进制
                                        }

                                        R66455_txtRegData.Text = tmp;
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "寄存器\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x03:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma \t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x04:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Gamma \tOTP OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma \tOTP ERROR！\r\n";
                                    }
                                    break;
                                case 0x05:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "DLL计算 \t OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "DLL计算 \t ERROR！\r\n";
                                    }
                                    break;
                                case 0x06:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "13p 写Gamma \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "13p 写Gamma \t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x07:  //read gamma
                                    {
                                        R6645x_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                        R6645x_R3.Value = Decimal.Parse((Rec_tmp[8] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                        R6645x_R5.Value = Decimal.Parse((Rec_tmp[12] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                        R6645x_R9.Value = Decimal.Parse((Rec_tmp[16] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                        R6645x_R16.Value = Decimal.Parse((Rec_tmp[20] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                        R6645x_R36.Value = Decimal.Parse((Rec_tmp[24] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                        R6645x_R64.Value = Decimal.Parse((Rec_tmp[28] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                        R6645x_R96.Value = Decimal.Parse((Rec_tmp[32] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                        R6645x_R128.Value = Decimal.Parse((Rec_tmp[36] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                        R6645x_R160.Value = Decimal.Parse((Rec_tmp[40] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                        R6645x_R200.Value = Decimal.Parse((Rec_tmp[44] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                        R6645x_R224.Value = Decimal.Parse((Rec_tmp[48] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                        R6645x_R255.Value = Decimal.Parse((Rec_tmp[50] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());

                                        R6645x_G0.Value = Decimal.Parse((Rec_tmp[52] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                        R6645x_G3.Value = Decimal.Parse((Rec_tmp[56] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                        R6645x_G5.Value = Decimal.Parse((Rec_tmp[60] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                        R6645x_G9.Value = Decimal.Parse((Rec_tmp[64] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                        R6645x_G16.Value = Decimal.Parse((Rec_tmp[68] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                        R6645x_G36.Value = Decimal.Parse((Rec_tmp[72] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[73].ToString());
                                        R6645x_G64.Value = Decimal.Parse((Rec_tmp[76] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[77].ToString());
                                        R6645x_G96.Value = Decimal.Parse((Rec_tmp[80] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[81].ToString());
                                        R6645x_G128.Value = Decimal.Parse((Rec_tmp[84] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[85].ToString());
                                        R6645x_G160.Value = Decimal.Parse((Rec_tmp[88] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[89].ToString());
                                        R6645x_G200.Value = Decimal.Parse((Rec_tmp[92] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[93].ToString());
                                        R6645x_G224.Value = Decimal.Parse((Rec_tmp[96] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[97].ToString());
                                        R6645x_G255.Value = Decimal.Parse((Rec_tmp[98] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[99].ToString());

                                        R6645x_B0.Value = Decimal.Parse((Rec_tmp[100] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[101].ToString());
                                        R6645x_B3.Value = Decimal.Parse((Rec_tmp[104] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[105].ToString());
                                        R6645x_B5.Value = Decimal.Parse((Rec_tmp[108] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[109].ToString());
                                        R6645x_B9.Value = Decimal.Parse((Rec_tmp[112] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[113].ToString());
                                        R6645x_B16.Value = Decimal.Parse((Rec_tmp[116] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[117].ToString());
                                        R6645x_B36.Value = Decimal.Parse((Rec_tmp[120] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[121].ToString());
                                        R6645x_B64.Value = Decimal.Parse((Rec_tmp[124] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[125].ToString());
                                        R6645x_B96.Value = Decimal.Parse((Rec_tmp[128] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[129].ToString());
                                        R6645x_B128.Value = Decimal.Parse((Rec_tmp[132] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[133].ToString());
                                        R6645x_B160.Value = Decimal.Parse((Rec_tmp[136] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[137].ToString());
                                        R6645x_B200.Value = Decimal.Parse((Rec_tmp[140] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[141].ToString());
                                        R6645x_B224.Value = Decimal.Parse((Rec_tmp[144] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[145].ToString());
                                        R6645x_B255.Value = Decimal.Parse((Rec_tmp[146] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[147].ToString());
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "读Gamma \t读 OK！\r\n";
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                        frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                        if (frmHelp.textBox1.Lines.Length > 50) frmHelp.textBox1.Text = null;
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }
        //------------------------------------------R66455_START------------------------------------------
        //Gamma保存
        private void R6645x_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = str + "R" + Point_R6645x[0].ToString() + "," + R6645x_R3.Text + "," + "G" + Point_R6645x[0].ToString() + "," + R6645x_G3.Text + "," + "B" + Point_R6645x[0].ToString() + "," + R6645x_B3.Text + "\r\n";
                    str = str + "R" + Point_R6645x[1].ToString() + "," + R6645x_R9.Text + "," + "G" + Point_R6645x[1].ToString() + "," + R6645x_G9.Text + "," + "B" + Point_R6645x[1].ToString() + "," + R6645x_B9.Text + "\r\n";
                    str = str + "R" + Point_R6645x[2].ToString() + "," + R6645x_R36.Text + "," + "G" + Point_R6645x[2].ToString() + "," + R6645x_G36.Text + "," + "B" + Point_R6645x[2].ToString() + "," + R6645x_B36.Text + "\r\n";
                    str = str + "R" + Point_R6645x[3].ToString() + "," + R6645x_R96.Text + "," + "G" + Point_R6645x[3].ToString() + "," + R6645x_G96.Text + "," + "B" + Point_R6645x[3].ToString() + "," + R6645x_B96.Text + "\r\n";
                    str = str + "R" + Point_R6645x[4].ToString() + "," + R6645x_R160.Text + "," + "G" + Point_R6645x[4].ToString() + "," + R6645x_G160.Text + "," + "B" + Point_R6645x[4].ToString() + "," + R6645x_B160.Text + "\r\n";
                    str = str + "R" + Point_R6645x[5].ToString() + "," + R6645x_R224.Text + "," + "G" + Point_R6645x[5].ToString() + "," + R6645x_G224.Text + "," + "B" + Point_R6645x[5].ToString() + "," + R6645x_B224.Text + "\r\n";
                    str = str + "R" + Point_R6645x[6].ToString() + "," + R6645x_R0.Text + "," + "G" + Point_R6645x[6].ToString() + "," + R6645x_G0.Text + "," + "B" + Point_R6645x[6].ToString() + "," + R6645x_B0.Text + "\r\n";
                    str = str + "R" + Point_R6645x[7].ToString() + "," + R6645x_R5.Text + "," + "G" + Point_R6645x[7].ToString() + "," + R6645x_G5.Text + "," + "B" + Point_R6645x[7].ToString() + "," + R6645x_B5.Text + "\r\n";
                    str = str + "R" + Point_R6645x[8].ToString() + "," + R6645x_R16.Text + "," + "G" + Point_R6645x[8].ToString() + "," + R6645x_G16.Text + "," + "B" + Point_R6645x[8].ToString() + "," + R6645x_B16.Text + "\r\n";
                    str = str + "R" + Point_R6645x[9].ToString() + "," + R6645x_R64.Text + "," + "G" + Point_R6645x[9].ToString() + "," + R6645x_G64.Text + "," + "B" + Point_R6645x[9].ToString() + "," + R6645x_B64.Text + "\r\n";
                    str = str + "R" + Point_R6645x[10].ToString() + "," + R6645x_R128.Text + "," + "G" + Point_R6645x[10].ToString() + "," + R6645x_G128.Text + "," + "B" + Point_R6645x[10].ToString() + "," + R6645x_B128.Text + "\r\n";
                    str = str + "R" + Point_R6645x[11].ToString() + "," + R6645x_R200.Text + "," + "G" + Point_R6645x[11].ToString() + "," + R6645x_G200.Text + "," + "B" + Point_R6645x[11].ToString() + "," + R6645x_B200.Text + "\r\n";
                    str = str + "R" + Point_R6645x[12].ToString() + "," + R6645x_R255.Text + "," + "G" + Point_R6645x[12].ToString() + "," + R6645x_G255.Text + "," + "B" + Point_R6645x[12].ToString() + "," + R6645x_B255.Text + "\r\n";
                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void R6645x_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                R6645x_R3.Text = arrtmp[1].ToString(); R6645x_G3.Text = arrtmp[3].ToString(); R6645x_B3.Text = arrtmp[5].ToString();
                R6645x_R9.Text = arrtmp[7].ToString(); R6645x_G9.Text = arrtmp[9].ToString(); R6645x_B9.Text = arrtmp[11].ToString();
                R6645x_R36.Text = arrtmp[13].ToString(); R6645x_G36.Text = arrtmp[15].ToString(); R6645x_B36.Text = arrtmp[17].ToString();
                R6645x_R96.Text = arrtmp[19].ToString(); R6645x_G96.Text = arrtmp[21].ToString(); R6645x_B96.Text = arrtmp[23].ToString();
                R6645x_R160.Text = arrtmp[25].ToString(); R6645x_G160.Text = arrtmp[27].ToString(); R6645x_B160.Text = arrtmp[29].ToString();
                R6645x_R224.Text = arrtmp[31].ToString(); R6645x_G224.Text = arrtmp[33].ToString(); R6645x_B224.Text = arrtmp[35].ToString();
                R6645x_R0.Text = arrtmp[37].ToString(); R6645x_G0.Text = arrtmp[39].ToString(); R6645x_B0.Text = arrtmp[41].ToString();
                R6645x_R5.Text = arrtmp[43].ToString(); R6645x_G5.Text = arrtmp[45].ToString(); R6645x_B5.Text = arrtmp[47].ToString();
                R6645x_R16.Text = arrtmp[49].ToString(); R6645x_G16.Text = arrtmp[51].ToString(); R6645x_B16.Text = arrtmp[53].ToString();
                R6645x_R64.Text = arrtmp[55].ToString(); R6645x_G64.Text = arrtmp[57].ToString(); R6645x_B64.Text = arrtmp[59].ToString();
                R6645x_R128.Text = arrtmp[61].ToString(); R6645x_G128.Text = arrtmp[63].ToString(); R6645x_B128.Text = arrtmp[65].ToString();
                R6645x_R200.Text = arrtmp[67].ToString(); R6645x_G200.Text = arrtmp[69].ToString(); R6645x_B200.Text = arrtmp[71].ToString();
                R6645x_R255.Text = arrtmp[73].ToString(); R6645x_G255.Text = arrtmp[75].ToString(); R6645x_B255.Text = arrtmp[77].ToString();
            }
        }
        private void rBt_R66455_normal_gamma_CheckedChanged(object sender, EventArgs e)
        {
            R66455_txtRegData.Text = "B0,00";
            R66455_GammaW_Click(R66455_RegW, null);
            Thread.Sleep(50);

            if (r6645x_cbo_index.SelectedIndex == 1) //R66455
            {
                R66455_txtRegData.Text = "38,00";
                R66455_GammaW_Click(R66455_RegW, null);
                Thread.Sleep(50);
                R66455_txtRegData.Text = "53,20";
                R66455_GammaW_Click(R66455_RegW, null);
                Thread.Sleep(50);
                if (rBt_R66455_normal_gamma.Checked == true)
                {
                    R66455_txtRegData.Text = R6645x_nor1_txt.Text; //  "51,0F,FF";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal2_gamma.Checked == true) //CAH
                {
                    R66455_txtRegData.Text = R6645x_nor2_txt.Text; //"51,05,2F";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal3_gamma.Checked == true) //C9H
                {
                    R66455_txtRegData.Text = R6645x_nor3_txt.Text;//"51,01,2D";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal4_gamma.Checked == true) //C8H
                {
                    R66455_txtRegData.Text = R6645x_nor4_txt.Text;//"51,00,88";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal5_gamma.Checked == true) //C7H
                {
                    R66455_txtRegData.Text = R6645x_nor5_txt.Text;//"51,00,FF";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                    if (cbx_Nor5_inter_AOD.Checked == true)
                    {
                        R66455_txtRegData.Text = "39,00";
                        R66455_GammaW_Click(R66455_RegW, null);
                        Thread.Sleep(50);
                    }
                }
                else if (rBt_R66455_HBM_gamma.Checked == true)
                {
                    R66455_txtRegData.Text = "53,C4";//E0
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                    R66455_txtRegData.Text = R6645x_nor0_txt.Text; //"51,0F,FF";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
            }
            else if (r6645x_cbo_index.SelectedIndex == 0) //R66451  6段gamma
            {
                R66455_txtRegData.Text = "38,00";
                R66455_GammaW_Click(R66455_RegW, null);
                Thread.Sleep(50);
                R66455_txtRegData.Text = "53,20";
                R66455_GammaW_Click(R66455_RegW, null);
                Thread.Sleep(50);

                if (rBt_R66455_normal_gamma.Checked == true)
                {
                    R66455_txtRegData.Text = R6645x_nor1_txt.Text;// "51,0F,86";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal2_gamma.Checked == true) //C9H
                {
                    R66455_txtRegData.Text = R6645x_nor2_txt.Text; //"51,02,6B";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal3_gamma.Checked == true) //C8H
                {
                    R66455_txtRegData.Text = R6645x_nor3_txt.Text; //"51,01,69";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal4_gamma.Checked == true) //C7H
                {
                    R66455_txtRegData.Text = R6645x_nor4_txt.Text; //"51,00,79";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal5_gamma.Checked == true) //C6H aod
                {
                    R66455_txtRegData.Text = R6645x_nor5_txt.Text; //"51,00,78";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                    if (cbx_Nor5_inter_AOD.Checked == true)
                    {
                        R66455_txtRegData.Text = "39,00";
                        R66455_GammaW_Click(R66455_RegW, null);
                        Thread.Sleep(50);
                    }
                }
                else if (rBt_R66455_HBM_gamma.Checked == true)
                {
                    R66455_txtRegData.Text = "53,C4";//E0
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                    R66455_txtRegData.Text = R6645x_nor0_txt.Text; //"51,0f,ff";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
            }
            else if (r6645x_cbo_index.SelectedIndex == 2) //R66456
            {
                R66455_txtRegData.Text = "88,00";
                R66455_GammaW_Click(R66455_RegW, null);
                Thread.Sleep(50);
                R66455_txtRegData.Text = "53,20";
                R66455_GammaW_Click(R66455_RegW, null);
                Thread.Sleep(50);
                if (rBt_R66455_normal_gamma.Checked == true)
                {
                    R66455_txtRegData.Text = R6645x_nor1_txt.Text;//"51,0F,FF";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal2_gamma.Checked == true) //CAH
                {
                    R66455_txtRegData.Text = R6645x_nor2_txt.Text; //"51,05,2F";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal3_gamma.Checked == true) //C9H
                {
                    R66455_txtRegData.Text = R6645x_nor3_txt.Text; //"51,01,2D";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal4_gamma.Checked == true) //C8H
                {
                    R66455_txtRegData.Text = R6645x_nor4_txt.Text; //"51,00,88";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                //------------------------------------------------------------------------------------
                if (rBt_R66455_normal6_gamma.Checked == true) //C5H
                {
                    R66455_txtRegData.Text = R6645x_nor6_txt.Text;//"51,00,88";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                if (rBt_R66455_normal7_gamma.Checked == true) //C3H
                {
                    R66455_txtRegData.Text = R6645x_nor7_txt.Text;//"51,00,88";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
                //----------------------------------------------------------------------------------

                if (rBt_R66455_normal5_gamma.Checked == true) //C7H
                {
                    R66455_txtRegData.Text = R6645x_nor5_txt.Text;// "51,00,FF";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                    if (cbx_Nor5_inter_AOD.Checked == true) //AOD gamma adjust in AOD mode or not
                    {
                        R66455_txtRegData.Text = "88,01";
                        R66455_GammaW_Click(R66455_RegW, null);
                        Thread.Sleep(50);
                    }
                }
                else if (rBt_R66455_HBM_gamma.Checked == true)
                {
                    R66455_txtRegData.Text = "53,C4";//E0
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                    R66455_txtRegData.Text = R6645x_nor0_txt.Text; //"51,0F,FF";
                    R66455_GammaW_Click(R66455_RegW, null);
                    Thread.Sleep(50);
                }
            }

            //  R66455_txtRegData.Clear();
        }
        private void R6645x_RGammaW_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 40 + 5;
            Send_buf = new byte[40 + 5];
            Send_buf[0] = 0x30; //R6645x
            Send_buf[1] = 0x06; //regW gamma 13 position
            Send_buf[2] = 40; //13*2+13+1cmd
            Send_buf[3] = 0;
            switch (btnTmp.Name)
            {
                case "R6645x_RGammaW":
                    {
                        Send_buf[3] |= 0x00; //red

                        Send_buf[4] = Point_R6645x[6]; //0
                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(R6645x_R0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(R6645x_R0.Value.ToString())) & 0xff);
                        Send_buf[7] = Point_R6645x[0]; //3
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(R6645x_R3.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(R6645x_R3.Value.ToString())) & 0xff);
                        Send_buf[10] = Point_R6645x[7];
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(R6645x_R5.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(R6645x_R5.Value.ToString())) & 0xff);
                        Send_buf[13] = Point_R6645x[1];
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(R6645x_R9.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(R6645x_R9.Value.ToString())) & 0xff);
                        Send_buf[16] = Point_R6645x[8];
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(R6645x_R16.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(R6645x_R16.Value.ToString())) & 0xff);
                        Send_buf[19] = Point_R6645x[2];
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(R6645x_R36.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(R6645x_R36.Value.ToString())) & 0xff);
                        Send_buf[22] = Point_R6645x[9];
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(R6645x_R64.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(R6645x_R64.Value.ToString())) & 0xff);
                        Send_buf[25] = Point_R6645x[3];
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(R6645x_R96.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(R6645x_R96.Value.ToString())) & 0xff);
                        Send_buf[28] = Point_R6645x[10];
                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(R6645x_R128.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(R6645x_R128.Value.ToString())) & 0xff);
                        Send_buf[31] = Point_R6645x[4];
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(R6645x_R160.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(R6645x_R160.Value.ToString())) & 0xff);
                        Send_buf[34] = Point_R6645x[11];
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(R6645x_R200.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(R6645x_R200.Value.ToString())) & 0xff);
                        Send_buf[37] = Point_R6645x[5];
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(R6645x_R224.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(R6645x_R224.Value.ToString())) & 0xff);
                        Send_buf[40] = Point_R6645x[12]; //255
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(R6645x_R255.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(R6645x_R255.Value.ToString())) & 0xff);
                    }
                    break;
                case "R6645x_GGammaW":
                    {
                        Send_buf[3] |= 0x01; //green

                        Send_buf[4] = Point_R6645x[6]; //0
                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(R6645x_G0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(R6645x_G0.Value.ToString())) & 0xff);
                        Send_buf[7] = Point_R6645x[0]; //3
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(R6645x_G3.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(R6645x_G3.Value.ToString())) & 0xff);
                        Send_buf[10] = Point_R6645x[7];
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(R6645x_G5.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(R6645x_G5.Value.ToString())) & 0xff);
                        Send_buf[13] = Point_R6645x[1];
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(R6645x_G9.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(R6645x_G9.Value.ToString())) & 0xff);
                        Send_buf[16] = Point_R6645x[8];
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(R6645x_G16.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(R6645x_G16.Value.ToString())) & 0xff);
                        Send_buf[19] = Point_R6645x[2];
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(R6645x_G36.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(R6645x_G36.Value.ToString())) & 0xff);
                        Send_buf[22] = Point_R6645x[9];
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(R6645x_G64.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(R6645x_G64.Value.ToString())) & 0xff);
                        Send_buf[25] = Point_R6645x[3];
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(R6645x_G96.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(R6645x_G96.Value.ToString())) & 0xff);
                        Send_buf[28] = Point_R6645x[10];
                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(R6645x_G128.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(R6645x_G128.Value.ToString())) & 0xff);
                        Send_buf[31] = Point_R6645x[4];
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(R6645x_G160.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(R6645x_G160.Value.ToString())) & 0xff);
                        Send_buf[34] = Point_R6645x[11];
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(R6645x_G200.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(R6645x_G200.Value.ToString())) & 0xff);
                        Send_buf[37] = Point_R6645x[5];
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(R6645x_G224.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(R6645x_G224.Value.ToString())) & 0xff);
                        Send_buf[40] = Point_R6645x[12]; //255
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(R6645x_G255.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(R6645x_G255.Value.ToString())) & 0xff);
                    }
                    break;
                case "R6645x_BGammaW":
                    {
                        Send_buf[3] |= 0x02; //BLUE

                        Send_buf[4] = Point_R6645x[6]; //0
                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(R6645x_B0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(R6645x_B0.Value.ToString())) & 0xff);
                        Send_buf[7] = Point_R6645x[0]; //3
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(R6645x_B3.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(R6645x_B3.Value.ToString())) & 0xff);
                        Send_buf[10] = Point_R6645x[7];
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(R6645x_B5.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(R6645x_B5.Value.ToString())) & 0xff);
                        Send_buf[13] = Point_R6645x[1];
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(R6645x_B9.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(R6645x_B9.Value.ToString())) & 0xff);
                        Send_buf[16] = Point_R6645x[8];
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(R6645x_B16.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(R6645x_B16.Value.ToString())) & 0xff);
                        Send_buf[19] = Point_R6645x[2];
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(R6645x_B36.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(R6645x_B36.Value.ToString())) & 0xff);
                        Send_buf[22] = Point_R6645x[9];
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(R6645x_B64.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(R6645x_B64.Value.ToString())) & 0xff);
                        Send_buf[25] = Point_R6645x[3];
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(R6645x_B96.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(R6645x_B96.Value.ToString())) & 0xff);
                        Send_buf[28] = Point_R6645x[10];
                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(R6645x_B128.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(R6645x_B128.Value.ToString())) & 0xff);
                        Send_buf[31] = Point_R6645x[4];
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(R6645x_B160.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(R6645x_B160.Value.ToString())) & 0xff);
                        Send_buf[34] = Point_R6645x[11];
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(R6645x_B200.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(R6645x_B200.Value.ToString())) & 0xff);
                        Send_buf[37] = Point_R6645x[5];
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(R6645x_B224.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(R6645x_B224.Value.ToString())) & 0xff);
                        Send_buf[40] = Point_R6645x[12]; //255
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(R6645x_B255.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(R6645x_B255.Value.ToString())) & 0xff);
                    }
                    break;
                case "R6645x_BGammaR":
                    {
                        //Send_buf[3] |= 0x02; //blue
                        Setting.TempRecord.cmdLen = 145 + 5;
                        Send_buf = new byte[145 + 5];
                        Send_buf[0] = 0x30; //R6645x
                        Send_buf[1] = 0x07; //regR gamma 13 position
                        Send_buf[2] = 145; //
                    }
                    break;
                default:
                    break;
            }
            //-------------------------------------------------------------------
            if (rBt_R66455_normal_gamma.Checked == true)
            {
                if (r6645x_cbo_index.SelectedIndex == 0) //R66451        // R66451.Checked == true) //r66451 cA
                { Send_buf[1] |= 0x20; }  //寄存器写 CA
                else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                { Send_buf[1] |= 0x10; } //寄存器写 cB
                else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                { Send_buf[1] |= 0x40; } //寄存器写 c8
            }
            else if (rBt_R66455_normal2_gamma.Checked == true)
            {
                if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                { Send_buf[1] |= 0x30; }  //寄存器写 C9
                else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                { Send_buf[1] |= 0x20; } //寄存器写 ca
                else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                { Send_buf[1] |= 0x50; } //寄存器写 c7
            }
            else if (rBt_R66455_normal3_gamma.Checked == true)
            {
                if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                { Send_buf[1] |= 0x40; }  //寄存器写 C8
                else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                { Send_buf[1] |= 0x30; } //寄存器写 c9
                else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                { Send_buf[1] |= 0x60; } //寄存器写 c6
            }
            else if (rBt_R66455_normal4_gamma.Checked == true)
            {
                if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                { Send_buf[1] |= 0x50; }  //寄存器写 C7
                else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                { Send_buf[1] |= 0x40; } //寄存器写 c8
                else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                { Send_buf[1] |= 0x80; } //寄存器写 c4  0x70=c5
            }
            else if (rBt_R66455_normal5_gamma.Checked == true)
            {
                if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                { Send_buf[1] |= 0x60; }  //寄存器写 C6
                else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                { Send_buf[1] |= 0x50; } //寄存器写 c7
                else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                { Send_buf[1] |= 0xC0; } //寄存器写 c0
            }
            else if (rBt_R66455_normal6_gamma.Checked == true)//R66456
            {
                Send_buf[1] |= 0x70;  //寄存器写 c5
            }
            else if (rBt_R66455_normal7_gamma.Checked == true)//R66456
            {
                Send_buf[1] |= 0x90;  //寄存器写 c3
            }

            else if (rBt_R66455_HBM_gamma.Checked == true)
            {
                if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                { Send_buf[1] |= 0x10; }  //寄存器写 CB
                else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                { Send_buf[1] |= 0x00; } //寄存器写 cC
                else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                { Send_buf[1] |= 0x30; } //寄存器写 c9
            }
            if (r6645x_cbo_index.SelectedIndex == 2)//R66456
            { Send_buf[3] |= 0x80; }
            else
            { Send_buf[3] |= 0x00; }


            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, Send_buf[2] + 5);   //R66455
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void Default_set_Click(object sender, EventArgs e)
        {
            R66455_RED.Value = 0xD00;
            R66455_GREEN.Value = 0xD00;
            R66455_BLUE.Value = 0xE00;
            R66455_Gary.Value = R66455_Gary.Maximum;
        }
        private void txt_R66455_lum_TextChanged(object sender, EventArgs e)
        {
            btnCal_R6645x_13Position_Click(null, null);
            btnCalculate_R66455_Click(null, null);
        }
        private void R66455_GammaW_Click(object sender, EventArgs e)
        {
            int j, i, ccH_data_i = 4, cBH_data_i = 4, cnt;
            byte DataNum;

            Button btnTmp = (Button)sender;
            Send_buf = new byte[13];
            Setting.TempRecord.cmdLen = 13;

            //-------------------gvo gamma point defined------------------------------------
            int X1_2_LV = 0, Y1_2_R = 0, Y1_2_G = 0, Y1_2_B = 0, X_temp;
            int[] X1_2_Buf = new int[31] {0,2,3,4,6,8,10,12,16,20,28,36,44,52,68,84,100,116,132,148,164,180,188,196,212,220,
                                        228,236,244,252,255};

            Algorithm_Option = new int[2] { 1, 0 };             // Option for algorithm, [0]: Fixed Tuning ON/OFF (0: OFF, 1: ON)
            Algorithm_Setting = new double[22] {			// Setting for algorithm
		            0,										// [0]: Fixed Tuning, level setting method(0: Automatic, 1: Manual)
	                64,64,64,								// [1]: Fixed Tuning, threshold setting for automatic method
		            2,2,2,				//推荐2					// [2]: Fixed Tuning, number of fixed point setting for automatic method
		            24,  48,  96, 128,  0,					// [ 3- 7]: Fixed Tuning, level setting for manual method for R
		            24,  48,  96, 128,  0,					// [ 8-12]: Fixed Tuning, level setting for manual method for G
		            24,  48,  96, 128,  0				// [13-17]: Fixed Tuning, level setting for manual method for B
            };
            if (r6645x_cbo_index.SelectedIndex == 0)
            {
                productName = "R66451";             // product selection, please set R6645x at this time.
            }
            else if (r6645x_cbo_index.SelectedIndex == 1)
            {
                productName = "R66455";
            }
            else if (r6645x_cbo_index.SelectedIndex == 2)
            {
                productName = "R66456";
            }

            Measured_VPLVL_VNLVL[0] = Convert.ToDouble(R6645x_V0.Value);    // V0
            Measured_VPLVL_VNLVL[1] = Convert.ToDouble(R6645x_V1023.Value);    // V1023
            //------------------------------------------------------------------------------
            Send_buf[0] = 0x30; //R66455

            switch (btnTmp.Name)
            {
                case "R66455_RegR":
                    {
                        string strLine = Convert.ToString(R66455_txtRegData.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组

                        DataNum = Convert.ToByte(tb_R66455_return_num.Text);
                        Setting.TempRecord.cmdLen = DataNum + 1 + 5;
                        Send_buf = new byte[DataNum + 1 + 5];

                        Send_buf[0] = 0x30;
                        Send_buf[1] = 0x02; //read function
                        Send_buf[2] = Convert.ToByte(DataNum + 1);//length
                        Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());//register
                    }
                    break;
                case "R66455_RegW":
                    {
                        try
                        {
                            string strLine = Convert.ToString(R66455_txtRegData.Text);
                            string[] arr = strLine.Split(','); //以逗号分割，添加到数组

                            Setting.TempRecord.cmdLen = arr.Length + 5;
                            Send_buf = new byte[arr.Length + 5];
                            Send_buf[0] = 0x30; //R66455
                            Send_buf[1] = 0x01; //寄存器写
                            Send_buf[2] = byte.Parse(arr.Length.ToString());
                            for (i = 0; i < arr.Length; i++)
                            {
                                Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                            }
                        }
                        catch
                        {
                            MessageBox.Show("输入格式有误！");
                        }
                    }
                    break;
                case "R66455_GammaW":
                    {
                        //Level(10bit)=LEVEL(8bit)*4+ROUNDDOWM(LEVEL(8bit)/64)
                        X_temp = Convert.ToByte(R66455_Gary.Value); //30-0
                        X1_2_LV = X1_2_Buf[X_temp] * 4 + (int)Math.Truncate(X1_2_Buf[X_temp] / 64.0);
                        //Voltage=(V0-V1023)*Y2_R/G/B/4092+V1023
                        Y1_2_R = Convert.ToInt16(R66455_RED.Value);
                        Y1_2_G = Convert.ToInt16(R66455_GREEN.Value);
                        Y1_2_B = Convert.ToInt16(R66455_BLUE.Value);

                        if (rBt_R66455_normal_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[1] = 0x23; }  //寄存器写 CA
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[1] = 0x13; } //寄存器写 cB
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[1] = 0x43; } //寄存器写 c8
                        }
                        else if (rBt_R66455_normal2_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[1] = 0x33; }  //寄存器写 C9
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[1] = 0x23; } //寄存器写 ca
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[1] = 0x53; } //寄存器写 c7
                        }
                        else if (rBt_R66455_normal3_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[1] = 0x43; }  //寄存器写 C8
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[1] = 0x33; } //寄存器写 c9
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[1] = 0x63; } //寄存器写 c6
                        }
                        else if (rBt_R66455_normal4_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[1] = 0x53; }  //寄存器写 C7
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[1] = 0x43; } //寄存器写 c8
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[1] = 0x83; } //寄存器写 c4
                        }
                        else if (rBt_R66455_normal5_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[1] = 0x63; }  //寄存器写 C6
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[1] = 0x53; } //寄存器写 c5
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[1] = 0xC3; } //寄存器写 C0
                        }
                        else if (rBt_R66455_normal6_gamma.Checked == true)
                        {
                            Send_buf[3] = 0x73;//R66456 C5
                        }
                        else if (rBt_R66455_normal7_gamma.Checked == true)
                        {
                            Send_buf[3] = 0x93;//R66456 C3
                        }
                        else if (rBt_R66455_HBM_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[1] = 0x13; }  //寄存器写 CB
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[1] = 0x03; } //寄存器写 CC
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[1] = 0x33; } //寄存器写 C9
                        }
                        Send_buf[2] = 0x08; //长度

                        Send_buf[3] = Convert.ToByte((Convert.ToUInt16(X1_2_LV) >> 8) & 0x03);
                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(X1_2_LV) & 0xff);

                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(Y1_2_R >> 8) & 0x0f));
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(Y1_2_R) & 0xff);

                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(Y1_2_G >> 8) & 0x0f));
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(Y1_2_G) & 0xff);

                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(Y1_2_B >> 8) & 0x0f));
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(Y1_2_B) & 0xff);

                        //-----------------------register convert to voltage----------------------------------------------------
                        Target_Level_NonLC[X_temp] = X1_2_LV;

                        Target_Voltage_R_NonLC[X_temp] = Convert.ToDouble((Measured_VPLVL_VNLVL[0] - Measured_VPLVL_VNLVL[1]) * (4092 - Y1_2_R) / 4092) + Measured_VPLVL_VNLVL[1];
                        /*
                        if (X_temp > 0)
                        {
                            if ((Target_Voltage_R_NonLC[X_temp] < Target_Voltage_R_NonLC[X_temp + 1]))
                            {
                                Target_Voltage_R_NonLC[X_temp] = Target_Voltage_R_NonLC[X_temp + 1] - (Target_Voltage_R_NonLC[X_temp + 2] - Target_Voltage_R_NonLC[X_temp + 1]) / (double)(Target_Level_NonLC[X_temp + 2] - Target_Level_NonLC[X_temp + 1]) * (double)(Target_Level_NonLC[X_temp + 1] - Target_Level_NonLC[X_temp]); ;
                            }
                        }
                        */
                        //---------------------------------------------------------------------------------------------------------------------------------
                        Target_Voltage_G_NonLC[X_temp] = Convert.ToDouble((Measured_VPLVL_VNLVL[0] - Measured_VPLVL_VNLVL[1]) * (4092 - Y1_2_G) / 4092) + Measured_VPLVL_VNLVL[1];
                        if (X_temp > 0)
                        {
                            if ((Target_Voltage_G_NonLC[X_temp] < Target_Voltage_G_NonLC[X_temp + 1]))
                            {
                                Target_Voltage_G_NonLC[X_temp] = Target_Voltage_G_NonLC[X_temp + 1] - (Target_Voltage_G_NonLC[X_temp + 2] - Target_Voltage_G_NonLC[X_temp + 1]) / (Target_Level_NonLC[X_temp + 2] - Target_Level_NonLC[X_temp + 1]) * (Target_Level_NonLC[X_temp + 1] - Target_Level_NonLC[X_temp]);
                            }
                        }
                        //----------------------------------------------------------------------------------------------------------------------------------
                        Target_Voltage_B_NonLC[X_temp] = Convert.ToDouble((Measured_VPLVL_VNLVL[0] - Measured_VPLVL_VNLVL[1]) * (4092 - Y1_2_B) / 4092) + Measured_VPLVL_VNLVL[1];
                        /*
                        if (X_temp > 0)
                        {
                            if ((Target_Voltage_B_NonLC[X_temp] < Target_Voltage_B_NonLC[X_temp + 1]))
                            {
                                Target_Voltage_B_NonLC[X_temp] = Target_Voltage_B_NonLC[X_temp + 1] - (Target_Voltage_B_NonLC[X_temp + 2] - Target_Voltage_B_NonLC[X_temp + 1]) / (double)(Target_Level_NonLC[X_temp + 2] - Target_Level_NonLC[X_temp + 1]) * (double)(Target_Level_NonLC[X_temp + 1] - Target_Level_NonLC[X_temp]);
                            }
                        }
                        */
                        //------------------------------------------------------------------------------------------------------
                    }
                    break;
                case "R66455_9position_help":
                    {
                        ;
                    }
                    break;
                case "R66455_Gamma_DLL":
                    {
                        Send_buf = new byte[145 + 5];
                        Setting.TempRecord.cmdLen = 145 + 5;
                        Send_buf[0] = 0x30; //R66455
                        Send_buf[1] = 0x05; //Dll换算
                        Send_buf[2] = 145; //cmd+data

                        Initialize_Product();

                        if (rBt_R66455_normal_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[3] = 0xCA; }  //寄存器写 CA
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[3] = 0xCB; } //寄存器写 cB
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[3] = 0xC8; } //寄存器写 c8
                        }
                        else if (rBt_R66455_normal2_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[3] = 0xC9; }  //寄存器写 C9
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[3] = 0xCA; } //寄存器写 cA
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[3] = 0xC7; } //寄存器写 c7
                        }
                        else if (rBt_R66455_normal3_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[3] = 0xC8; }  //寄存器写 C8
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[3] = 0xC9; } //寄存器写 C9
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[3] = 0xC6; } //寄存器写 c6
                        }
                        else if (rBt_R66455_normal4_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[3] = 0xC7; }  //寄存器写 C7
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[3] = 0xC8; } //寄存器写 C8
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[3] = 0xC4; } //寄存器写 c5
                        }
                        else if (rBt_R66455_normal5_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[3] = 0xC6; }  //寄存器写 C6
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[3] = 0xC7; } //寄存器写 C7
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[3] = 0xC0; } //寄存器写 c0
                        }
                        else if (rBt_R66455_normal6_gamma.Checked == true)
                        {
                            Send_buf[3] = 0xC5;//R66456
                        }
                        else if (rBt_R66455_normal7_gamma.Checked == true)
                        {
                            Send_buf[3] = 0xC3;//R66456
                        }
                        else if (rBt_R66455_HBM_gamma.Checked == true)
                        {
                            //----------------------------------------------------------------------
                            // Set data for HBM mode
                            //----------------------------------------------------------------------
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { Send_buf[3] = 0xCB; }  //寄存器写 CB
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { Send_buf[3] = 0xCC; } //寄存器写 CC
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { Send_buf[3] = 0xC9; } //寄存器写 c9
                        }
                        //30->255   test
                        // ---------------------------------
                        for (int move = 1; move < Parttern_R66455.Length + 1 - 2; move++)
                        {
                            Target_Level_NonLC[move] = Target_Level_NonLC[move + 2];
                            if (move == 1)
                            {
                                Target_Level_NonLC[move] = 8;
                            }
                        }
                        Target_Level_NonLC[Parttern_R66455.Length - 1] = 0;
                        Target_Level_NonLC[Parttern_R66455.Length] = 0;

                        for (int move = 1; move < Parttern_R66455.Length + 1 - 2; move++)
                        {
                            Target_Voltage_R_NonLC[move] = Target_Voltage_R_NonLC[move + 2];
                        }
                        Target_Voltage_R_NonLC[Parttern_R66455.Length - 1] = 0;
                        Target_Voltage_R_NonLC[Parttern_R66455.Length] = 0;

                        for (int move = 1; move < Parttern_R66455.Length + 1 - 2; move++)
                        {
                            Target_Voltage_G_NonLC[move] = Target_Voltage_G_NonLC[move + 2];
                        }
                        Target_Voltage_G_NonLC[Parttern_R66455.Length - 1] = 0;
                        Target_Voltage_G_NonLC[Parttern_R66455.Length] = 0;

                        for (int move = 1; move < Parttern_R66455.Length + 1 - 2; move++)
                        {
                            Target_Voltage_B_NonLC[move] = Target_Voltage_B_NonLC[move + 2];
                        }
                        Target_Voltage_B_NonLC[Parttern_R66455.Length - 1] = 0;
                        Target_Voltage_B_NonLC[Parttern_R66455.Length] = 0;
                        // -------------------------------------

                        //----------------------------------------------------------------------
                        // Call DLL function (2D Adjustment)
                        //----------------------------------------------------------------------
                        if ((rBt_R66455_normal_gamma.Checked == true) || (rBt_R66455_normal5_gamma.Checked == true)) //AOD & normal
                        {
                            Algorithm_Option[0] = 1;    // [0]: Fixed Tuning ON/OFF (0: OFF, 1: ON)
                            Algorithm_Option[1] = 0;    // [1]: Target Voltage Correction ON/OFF (0: OFF, 1: ON)
                            Adjust2DXYParam_Voltage(
                                //===== Input =====
                                productName,                // product selection
                                Target_Level_NonLC,         // Target Level Non Linear Complete
                                Target_Voltage_R_NonLC,     // Target of Voltage R Non Linear Complete
                                Target_Voltage_G_NonLC,     // Target of Voltage G Non Linear Complete
                                Target_Voltage_B_NonLC,     // Target of Voltage B Non Linear Complete
                                Measured_VPLVL_VNLVL,       // V0/V1023 Voltage, [0]: V0, [1]: V1023
                                Algorithm_Option,           // Option for algorithm
                                Algorithm_Setting,          // Setting for algorithm
                                                            //===== Output =====
                                Adjusted_Registers          // Register settings after adjustment
                            );
                            // Mapping for register
                            Adjusted_REV = Adjusted_Registers[0];
                            for (i = 0; i < 13; i++)
                            {
                                Adjusted_X_R_CB[i] = Adjusted_Registers[1 + i * 2 + 13 * 0 + 0];
                                Adjusted_Y_R_CB[i] = Adjusted_Registers[1 + i * 2 + 13 * 0 + 1];
                                Adjusted_X_G_CB[i] = Adjusted_Registers[1 + i * 2 + 13 * 2 + 0];
                                Adjusted_Y_G_CB[i] = Adjusted_Registers[1 + i * 2 + 13 * 2 + 1];
                                Adjusted_X_B_CB[i] = Adjusted_Registers[1 + i * 2 + 13 * 4 + 0];
                                Adjusted_Y_B_CB[i] = Adjusted_Registers[1 + i * 2 + 13 * 4 + 1];
                            }
                            // Use 2D adjusted X register setting for 1D tuning, copy Adjusted_Registers to Measured_Registers
                            for (i = 0; i < 79; i++)
                            {
                                Measured_Registers[i] = Adjusted_Registers[i];
                            }
                        }
                        //--------------------------------------------------------------------------------------------
                        //HBM & GAMMA2~4
                        else if ((rBt_R66455_HBM_gamma.Checked == true) || (rBt_R66455_normal2_gamma.Checked == true) || (rBt_R66455_normal3_gamma.Checked == true) || (rBt_R66455_normal4_gamma.Checked == true))
                        {
                            // Algorithm Option & Setting
                            Algorithm_Option[1] = 0;	// [1]: Target Voltage Correction ON/OFF (0: OFF, 1: ON)
                            //----------------------------------------------------------------------
                            // Call DLL function (1D Adjustment)
                            //----------------------------------------------------------------------
                            Adjust1p5DXYParam_Voltage(
                                //===== Input =====
                                productName,                // product selection
                                Target_Level_NonLC,         // Target Level Non Linear Complete
                                Target_Voltage_R_NonLC,     // Target of Voltage R Non Linear Complete
                                Target_Voltage_G_NonLC,     // Target of Voltage G Non Linear Complete
                                Target_Voltage_B_NonLC,     // Target of Voltage B Non Linear Complete
                                Measured_VPLVL_VNLVL,       // V0/V1023 Voltage, [0]: V0, [1]: V1023
                                Measured_Registers,         // Register settings before adjustment
                                Algorithm_Option,			// Option for algorithm
                                Algorithm_Setting,          // Setting for algorithm
                                                            //===== Output =====
                                Adjusted_Registers          // Register settings after adjustment
                            );

                            // Mapping for register
                            Adjusted_REV = Adjusted_Registers[0];
                            for (i = 0; i < 13; i++)
                            {
                                Adjusted_X_R_CC[i] = Adjusted_Registers[1 + i * 2 + 13 * 0 + 0];
                                Adjusted_Y_R_CC[i] = Adjusted_Registers[1 + i * 2 + 13 * 0 + 1];
                                Adjusted_X_G_CC[i] = Adjusted_Registers[1 + i * 2 + 13 * 2 + 0];
                                Adjusted_Y_G_CC[i] = Adjusted_Registers[1 + i * 2 + 13 * 2 + 1];
                                Adjusted_X_B_CC[i] = Adjusted_Registers[1 + i * 2 + 13 * 4 + 0];
                                Adjusted_Y_B_CC[i] = Adjusted_Registers[1 + i * 2 + 13 * 4 + 1];
                            }
                        }
                        //----------------------------------------------------------------------
                        // Write adjusted register settings
                        //----------------------------------------------------------------------
                        // Create Output Text File
                        System.IO.StreamWriter sw = new System.IO.StreamWriter("丁伟_adjusted_register.txt");
                        sw.AutoFlush = true;
                        System.IO.TextWriter tw = System.IO.TextWriter.Synchronized(sw);
                        Console.SetOut(tw);

                        Console.Write("==============================CA310_Measure============================\n");
                        for (i = 0; i < 31; i++)
                        {
                            Console.Write("R66455_GAMMA_GRAY={0};     CA310_LUM={1}     CA310_X={2};    CA310_Y={3}\n", R66455_GAMMA_GRAY[30 - i], R66455_CA310_LUM[i], R66455_CA310_X[i], R66455_CA310_Y[i]);
                        }

                        Console.Write("==================================INPUT=================================\n");
                        for (i = 0; i < 31; i++)
                        {
                            Console.Write("Target_Level_NonLC[{0}]: {1};    Target_Voltage_R_NonLC[{0}]: {2};    Target_Voltage_G_NonLC[{0}]: {3};    Target_Voltage_R_NonLC[{0}]: {4};\n", 30 - i, Target_Level_NonLC[30 - i], Target_Voltage_R_NonLC[30 - i], Target_Voltage_G_NonLC[30 - i], Target_Voltage_B_NonLC[30 - i]);
                        }

                        Console.Write("==================================OUTPUT=================================\n");
                        for (i = 0; i < 79; i++)
                        {
                            Console.Write("Adjusted_Registers[{0}]: {1}\n", i, Adjusted_Registers[i]);
                        }

                        cnt = 1;
                        Console.WriteLine("Adjusted register setting");
                        // REV
                        Console.Write("REV Setting - C1h, please make C1h P1 register setting according to match with your other setting\n");
                        Console.Write("REV={0:D1}\n", Adjusted_REV);
                        // Flexible Gamma

                        if ((rBt_R66455_normal_gamma.Checked == true) || (rBt_R66455_normal5_gamma.Checked == true))
                        {
                            Console.Write("Flexible Gamma Setting - CBh\n");
                            // R register
                            for (i = 0; i < 13; i++)
                            {
                                if (1 <= i && i <= 11)
                                {
                                    Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                    cnt++, Adjusted_X_R_CB[i].ToString("x4").Substring(0, 2),
                                    cnt++, Adjusted_X_R_CB[i].ToString("x4").Substring(2, 2));

                                    Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_X_R_CB[i] >> 8);
                                    Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_X_R_CB[i] & 0xff);
                                }
                                Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                cnt++, Adjusted_Y_R_CB[i].ToString("x4").Substring(0, 2),
                                cnt++, Adjusted_Y_R_CB[i].ToString("x4").Substring(2, 2));
                                Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_Y_R_CB[i] >> 8);
                                Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_Y_R_CB[i] & 0xff);

                            }
                            // G register
                            for (i = 0; i < 13; i++)
                            {
                                if (1 <= i && i <= 11)
                                {
                                    Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                    cnt++, Adjusted_X_G_CB[i].ToString("x4").Substring(0, 2),
                                    cnt++, Adjusted_X_G_CB[i].ToString("x4").Substring(2, 2));
                                    Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_X_G_CB[i] >> 8);
                                    Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_X_G_CB[i] & 0xff);
                                }
                                Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                cnt++, Adjusted_Y_G_CB[i].ToString("x4").Substring(0, 2),
                                cnt++, Adjusted_Y_G_CB[i].ToString("x4").Substring(2, 2));
                                Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_Y_G_CB[i] >> 8);
                                Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_Y_G_CB[i] & 0xff);
                            }
                            // B register
                            for (i = 0; i < 13; i++)
                            {
                                if (1 <= i && i <= 11)
                                {
                                    Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                    cnt++, Adjusted_X_B_CB[i].ToString("x4").Substring(0, 2),
                                    cnt++, Adjusted_X_B_CB[i].ToString("x4").Substring(2, 2));
                                    Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_X_B_CB[i] >> 8);
                                    Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_X_B_CB[i] & 0xff);
                                }
                                Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                cnt++, Adjusted_Y_B_CB[i].ToString("x4").Substring(0, 2),
                                cnt++, Adjusted_Y_B_CB[i].ToString("x4").Substring(2, 2));
                                Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_Y_B_CB[i] >> 8);
                                Send_buf[ccH_data_i++] = Convert.ToByte(Adjusted_Y_B_CB[i] & 0xff);
                            }
                        }
                        //----------------------------------------------------------------------------------------
                        else if ((rBt_R66455_HBM_gamma.Checked == true) || (rBt_R66455_normal2_gamma.Checked == true) || (rBt_R66455_normal3_gamma.Checked == true) || (rBt_R66455_normal4_gamma.Checked == true))
                        {
                            // Flexible Gamma, CCh
                            cnt = 1;
                            Console.Write("Flexible Gamma Setting - CCh\n");
                            // R register
                            for (i = 0; i < 13; i++)
                            {
                                if (1 <= i && i <= 11)
                                {
                                    Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                        cnt++, Adjusted_X_R_CC[i].ToString("x4").Substring(0, 2),
                                        cnt++, Adjusted_X_R_CC[i].ToString("x4").Substring(2, 2));
                                    Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_X_R_CC[i] >> 8);
                                    Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_X_R_CC[i] & 0xff);
                                }
                                Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                    cnt++, Adjusted_Y_R_CC[i].ToString("x4").Substring(0, 2),
                                    cnt++, Adjusted_Y_R_CC[i].ToString("x4").Substring(2, 2));
                                Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_Y_R_CC[i] >> 8);
                                Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_Y_R_CC[i] & 0xff);
                            }
                            // G register
                            for (i = 0; i < 13; i++)
                            {
                                if (1 <= i && i <= 11)
                                {
                                    Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                        cnt++, Adjusted_X_G_CC[i].ToString("x4").Substring(0, 2),
                                        cnt++, Adjusted_X_G_CC[i].ToString("x4").Substring(2, 2));
                                    Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_X_G_CC[i] >> 8);
                                    Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_X_G_CC[i] & 0xff);
                                }
                                Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                    cnt++, Adjusted_Y_G_CC[i].ToString("x4").Substring(0, 2),
                                    cnt++, Adjusted_Y_G_CC[i].ToString("x4").Substring(2, 2));
                                Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_Y_G_CC[i] >> 8);
                                Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_Y_G_CC[i] & 0xff);
                            }
                            // B register
                            for (i = 0; i < 13; i++)
                            {
                                if (1 <= i && i <= 11)
                                {
                                    Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                        cnt++, Adjusted_X_B_CC[i].ToString("x4").Substring(0, 2),
                                        cnt++, Adjusted_X_B_CC[i].ToString("x4").Substring(2, 2));
                                    Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_X_B_CC[i] >> 8);
                                    Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_X_B_CC[i] & 0xff);
                                }
                                Console.Write("P{0:D3},{1:H2}\nP{2:D3},{3:H2}\n",
                                    cnt++, Adjusted_Y_B_CC[i].ToString("x4").Substring(0, 2),
                                    cnt++, Adjusted_Y_B_CC[i].ToString("x4").Substring(2, 2));
                                Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_Y_B_CC[i] >> 8);
                                Send_buf[cBH_data_i++] = Convert.ToByte(Adjusted_Y_B_CC[i] & 0xff);
                            }
                        }
                        //----------------------------------------------------------------------------------------

                        sw.Dispose();
                        //------------------------------------------------------------------------------------------
                        if (rBt_R66455_normal_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { R66455_txtRegData.Text = "CA"; }
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { R66455_txtRegData.Text = "CB"; }
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { R66455_txtRegData.Text = "C8"; }
                        }
                        else if (rBt_R66455_normal2_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { R66455_txtRegData.Text = "C9"; }
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { R66455_txtRegData.Text = "CA"; }
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { R66455_txtRegData.Text = "C7"; }
                        }
                        else if (rBt_R66455_normal3_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { R66455_txtRegData.Text = "C8"; }
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { R66455_txtRegData.Text = "C9"; }
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { R66455_txtRegData.Text = "C6"; }
                        }
                        else if (rBt_R66455_normal4_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { R66455_txtRegData.Text = "C7"; }
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { R66455_txtRegData.Text = "C8"; }
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { R66455_txtRegData.Text = "C4"; }
                        }
                        else if (rBt_R66455_normal5_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { R66455_txtRegData.Text = "C6"; }
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { R66455_txtRegData.Text = "C7"; }
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { R66455_txtRegData.Text = "C0"; }
                        }

                        else if (rBt_R66455_normal6_gamma.Checked == true)
                        {
                            R66455_txtRegData.Text = "C5";
                        }
                        else if (rBt_R66455_normal7_gamma.Checked == true)
                        {
                            R66455_txtRegData.Text = "C3";
                        }

                        else if (rBt_R66455_HBM_gamma.Checked == true)
                        {
                            if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                            { R66455_txtRegData.Text = "CB"; }
                            else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                            { R66455_txtRegData.Text = "CC"; }
                            else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                            { R66455_txtRegData.Text = "C9"; }
                        }
                        for (i = 4; i < 149; i++)
                        {
                            R66455_txtRegData.Text += ",";
                            R66455_txtRegData.Text += Send_buf[i].ToString("x2");
                        }
                        //------------------------------------------------------------------------------------------
                    }
                    break;
                case "R66455_IR_Drop_DLL":
                    {
                        //  callAdjustIRDrop();
                        //===== Output =====
                        int NumOfRegisters = 32;                // Register settings after djustment
                        int[] Registers = new int[108];      // Register settings after djustment
                        int[] others = { 1, 0, 0, 0 };                       //other value
                        string ProductID;
                        //===== Output =====
                        if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                        { ProductID = "R66451"; }  // Product ID(e.g. "R66451", "R66455","R66456")
                        else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                        { ProductID = "R66455"; }
                        else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                        { ProductID = "R66456"; }
                        else
                        { ProductID = "R66451"; }

                        int xsize = 1080;                                //Horizontal resolution of Real RGB.
                        int ysize = 2340;                                //Vertical resolution.
                        int DBV = 4095;                                  //DBV
                        int SPRtype = 0;                                 //0:SPR RGB, 1:SPR RGBG
                        int ErrorType = 0;                               //Error Type for Panel Brightness Uniformity.
                                                                         // (0:Standard deviation, 1:Max-Min, 2:Total Error)
                        double gamma = 2.2;                              //Gamma value (e.g. 2.2)

                        //----------------------------------------------------------------------
                        // Call DLL function
                        //----------------------------------------------------------------------
                        /*
                        double[,] MeasDataOfGray = new double[7, 68] { {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 },
                                                             {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 },
                                                            {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ,-1 },
                                                             {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1  },
                                                            {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ,-1,-1,-1,-1 },
                                                            {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ,-1 },
                                                            {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                                                             -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ,-1}};
                                                             */

                        if (FlexGammaSetting[25] != 0x3ff)
                        {
                            R6645x_load_gamma.BackColor = Color.Red;
                            MessageBox.Show("请导入正确的Gamma值!", "系统提示：");
                        }
                        else
                        {
                            IRDropAdjust(
                            //output
                            ref NumOfRegisters,         //Number of registers.
                            Registers,              //Register parameter (e.g. D3 01 FF FF FF 00 20 40 ...)
                                                    //input
                            ProductID,                  //Product ID (e.g. "R66451" , "R66455" )
                            xsize,                      //Horizontal resolution of Real RGB.
                            ysize,                      //Vertical resolution.
                            DBV,                        //DBV
                            SPRtype,                    //0:SPR RGB, 1:SPR RGBG
                            ErrorType,                  //Error Type for Panel Brightness Uniformity.
                                                        // (0:Standard deviation, 1:Max-Min, 2:Total Error)
                            gamma,                      //Gamma value (e.g. 2.2)
                            FlexGammaSetting,       //Flexible Gamma Setting.
                                                    // (e.g. REV, 0, R_CP00Y, R_CP01X, R_CP01Y, ...)
                            MeasDataOf9Pos,             //Measurement data of 9 positions. 
                                                        // 0,1,2:far, 3,4,5:mid, 6,7,8:near. format is x,y,Lv. (e.g. 0.3,0.31,430,...)
                                                        // (use 255 gray white)
                            MeasDataOfGray,             //Measurement data of WRGBCMY Gray.
                                                        // [7] means 0:W,1:R,2:G,3:B,4:C,5:M,6:Y.
                                                        // format is x,y,Lv,gray. (need 0, 128, 255 gray level)
                            others                      //other value
                            );
                            // Create Output Text File
                            System.IO.StreamWriter sw0 = new System.IO.StreamWriter("唐韬IR-Drop Compensation Registers.txt");
                            sw0.AutoFlush = true;
                            System.IO.TextWriter tw0 = System.IO.TextWriter.Synchronized(sw0);
                            Console.SetOut(tw0);
                            //----------------------------------------------------------------------
                            // Write adjusted register settings
                            //----------------------------------------------------------------------
                            // 1D3h : IR-Drop Compensation Registers
                            Console.WriteLine("##################################################");
                            Console.WriteLine("#1D3h : IR-Drop Compensation Registers");
                            Console.WriteLine("##################################################");

                            Console.WriteLine("mipi.write 0x29 0xB0 0x84");
                            R66455_txtRegData.Text = "B0,84\r\n";

                            R66455_txtRegData.AppendText("D3");
                            for (int k = 0; k < NumOfRegisters; k++)
                            {
                                Console.Write("0x{0:x2} ", Registers[k]);

                                R66455_txtRegData.AppendText("," + Registers[k].ToString("x2"));
                            }
                            Console.WriteLine("");
                            Console.WriteLine("mipi.write 0x29 0xB0 0x04");
                            R66455_txtRegData.AppendText("\r\nB0,04\r\n");
                            sw0.Dispose();
                        }
                        R6645x_load_gamma.BackColor = Color.Red;
                    }
                    break;
                case "R66455_Gamma_otp":
                    {
                        Setting.TempRecord.cmdLen = 2 + 5;
                        Send_buf = new byte[2 + 5];

                        Send_buf[0] = 0x30;
                        Send_buf[1] = 0x04; //otp function
                        Send_buf[2] = 0x02;//length

                        if (r6645x_cbo_index.SelectedIndex == 0) //R66451        
                        { Send_buf[3] = 0x00; }
                        else if (r6645x_cbo_index.SelectedIndex == 1)//R66455
                        { Send_buf[3] = 0x01; }
                        else if (r6645x_cbo_index.SelectedIndex == 2)//R66456
                        { Send_buf[3] = 0x02; }
                    }
                    break;
                case "R66455_Gary":
                    {
                        R66455_label.Text = Target_Level[Convert.ToByte(R66455_Gary.Value)].ToString();
                    }
                    break;
                case "btn_Sy_IR_Measure":
                    {
                        //   Msr_CA310();
                        R6645x_Measure_Information();//auto_lum, auto_x, auto_y
                        Send_buf[2] = 0x02;//length
                    }
                    break;
                case "btn_Sy_IR_Down":
                    {
                        SaveFileDialog sf = new SaveFileDialog(); //对话框
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

                        string str = "序号,x坐标,y坐标,亮度\r\n";
                        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                        {
                            if (sf.FileName == "") return; //如果没有输入文件名
                            try
                            {
                                StreamWriter sw = null; //文件流
                                sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                                for (int i0 = 0; i0 < (this.R6645x_dataGridView0.Rows.Count - 1); i0++)
                                {
                                    str = str + this.R6645x_dataGridView0.Rows[i0].Cells[0].Value.ToString() + ","
                                        + this.R6645x_dataGridView0.Rows[i0].Cells[1].Value.ToString() + ","
                                        + this.R6645x_dataGridView0.Rows[i0].Cells[2].Value.ToString() + ","
                                        + this.R6645x_dataGridView0.Rows[i0].Cells[3].Value.ToString() + ","
                                        + this.R6645x_dataGridView0.Rows[i0].Cells[4].Value.ToString() + "\r\n";
                                }
                                for (int i0 = 0; i0 < (this.R6645x_dataGridView1.Rows.Count - 1); i0++)
                                {
                                    str = str + this.R6645x_dataGridView1.Rows[i0].Cells[0].Value.ToString() + ","
                                        + this.R6645x_dataGridView1.Rows[i0].Cells[1].Value.ToString() + ","
                                        + this.R6645x_dataGridView1.Rows[i0].Cells[2].Value.ToString() + ","
                                        + this.R6645x_dataGridView1.Rows[i0].Cells[3].Value.ToString() + ","
                                        + this.R6645x_dataGridView1.Rows[i0].Cells[4].Value.ToString() + "\r\n";
                                }
                                for (int i0 = 0; i0 < (this.R6645x_dataGridView2.Rows.Count - 1); i0++)
                                {
                                    str = str + this.R6645x_dataGridView2.Rows[i0].Cells[0].Value.ToString() + ","
                                        + this.R6645x_dataGridView2.Rows[i0].Cells[1].Value.ToString() + ","
                                        + this.R6645x_dataGridView2.Rows[i0].Cells[2].Value.ToString() + ","
                                        + this.R6645x_dataGridView2.Rows[i0].Cells[3].Value.ToString() + ","
                                        + this.R6645x_dataGridView2.Rows[i0].Cells[4].Value.ToString() + "\r\n";
                                }
                                for (int i0 = 0; i0 < (this.R6645x_dataGridView3.Rows.Count - 1); i0++)
                                {
                                    str = str + this.R6645x_dataGridView3.Rows[i0].Cells[0].Value.ToString() + ","
                                        + this.R6645x_dataGridView3.Rows[i0].Cells[1].Value.ToString() + ","
                                        + this.R6645x_dataGridView3.Rows[i0].Cells[2].Value.ToString() + ","
                                        + this.R6645x_dataGridView3.Rows[i0].Cells[3].Value.ToString() + ","
                                        + this.R6645x_dataGridView3.Rows[i0].Cells[4].Value.ToString() + "\r\n";
                                }
                                for (int i0 = 0; i0 < (this.R6645x_dataGridView4.Rows.Count - 1); i0++)
                                {
                                    str = str + this.R6645x_dataGridView4.Rows[i0].Cells[0].Value.ToString() + ","
                                        + this.R6645x_dataGridView4.Rows[i0].Cells[1].Value.ToString() + ","
                                        + this.R6645x_dataGridView4.Rows[i0].Cells[2].Value.ToString() + ","
                                        + this.R6645x_dataGridView4.Rows[i0].Cells[3].Value.ToString() + ","
                                        + this.R6645x_dataGridView4.Rows[i0].Cells[4].Value.ToString() + "\r\n";
                                }
                                for (int i0 = 0; i0 < (this.R6645x_dataGridView5.Rows.Count - 1); i0++)
                                {
                                    str = str + this.R6645x_dataGridView5.Rows[i0].Cells[0].Value.ToString() + ","
                                        + this.R6645x_dataGridView5.Rows[i0].Cells[1].Value.ToString() + ","
                                        + this.R6645x_dataGridView5.Rows[i0].Cells[2].Value.ToString() + ","
                                        + this.R6645x_dataGridView5.Rows[i0].Cells[3].Value.ToString() + ","
                                        + this.R6645x_dataGridView5.Rows[i0].Cells[4].Value.ToString() + "\r\n";
                                }
                                for (int i0 = 0; i0 < (this.R6645x_dataGridView6.Rows.Count - 1); i0++)
                                {
                                    str = str + this.R6645x_dataGridView6.Rows[i0].Cells[0].Value.ToString() + ","
                                        + this.R6645x_dataGridView6.Rows[i0].Cells[1].Value.ToString() + ","
                                        + this.R6645x_dataGridView6.Rows[i0].Cells[2].Value.ToString() + ","
                                        + this.R6645x_dataGridView6.Rows[i0].Cells[3].Value.ToString() + ","
                                        + this.R6645x_dataGridView6.Rows[i0].Cells[4].Value.ToString() + "\r\n";
                                }
                                for (int i0 = 0; i0 < (this.R6645x_dataGridView.Rows.Count - 1); i0++)
                                {
                                    str = str + this.R6645x_dataGridView.Rows[i0].Cells[0].Value.ToString() + ","
                                        + this.R6645x_dataGridView.Rows[i0].Cells[1].Value.ToString() + ","
                                        + this.R6645x_dataGridView.Rows[i0].Cells[2].Value.ToString() + ","
                                        + this.R6645x_dataGridView.Rows[i0].Cells[3].Value.ToString() + ","
                                        + this.R6645x_dataGridView.Rows[i0].Cells[4].Value.ToString() + "\r\n";
                                }
                                sw.WriteLine(str);
                                sw.Dispose(); //释放资源
                                sw.Close(); //关闭数据流
                                MessageBox.Show("CA310数据保存OK！", "系统提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据保存时发生错误，请再试！", "系统提示：");
                            }
                        }
                    }
                    break;
                case "btn_Sy_IR_Load":
                    {

                    }
                    break;
                case "btn_Sy_IR_Clear":
                    {
                        DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                        if (dr == DialogResult.OK)
                        {
                            if (R6645x_tabControl.SelectedIndex == 0)
                            {
                                this.R6645x_dataGridView0.Rows.Clear();
                            }
                            if (R6645x_tabControl.SelectedIndex == 1)
                            {
                                this.R6645x_dataGridView1.Rows.Clear();
                            }
                            if (R6645x_tabControl.SelectedIndex == 2)
                            {
                                this.R6645x_dataGridView2.Rows.Clear();
                            }
                            if (R6645x_tabControl.SelectedIndex == 3)
                            {
                                this.R6645x_dataGridView3.Rows.Clear();
                            }
                            if (R6645x_tabControl.SelectedIndex == 4)
                            {
                                this.R6645x_dataGridView4.Rows.Clear();
                            }
                            if (R6645x_tabControl.SelectedIndex == 5)
                            {
                                this.R6645x_dataGridView5.Rows.Clear();
                            }
                            if (R6645x_tabControl.SelectedIndex == 6)
                            {
                                this.R6645x_dataGridView6.Rows.Clear();
                            }
                            if (R6645x_tabControl.SelectedIndex == 7)
                            {
                                this.R6645x_dataGridView.Rows.Clear();
                            }
                        }
                        else {; }
                    }
                    break;
                case "R6645x_load_gamma":
                    {
                        string strLine = Convert.ToString(R66455_txtRegData.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组
                        int cnt1 = 1;

                        if (arr.Length == 0x92)
                        {
                            for (int n = 0; n < 79; n++)
                            {
                                if (n == 0 || n == 1 || n == 27 || n == 53)
                                {
                                    FlexGammaSetting[n] = 0;
                                }
                                else if (n == 25 || n == 51 || n == 77)
                                {
                                    FlexGammaSetting[n] = 0x3ff;
                                }
                                else
                                {
                                    FlexGammaSetting[n] = (byte.Parse((Convert.ToInt32(arr[cnt1], 16)).ToString()) << 8) + byte.Parse((Convert.ToInt32(arr[cnt1 + 1], 16)).ToString());
                                    cnt1 = cnt1 + 2;
                                }
                            }
                            R6645x_load_gamma.BackColor = Color.Green;
                            MessageBox.Show("Gamma值载入ok!", "系统提示：");
                        }
                        else
                        {
                            MessageBox.Show("请导入正确的Gamma值!", "系统提示：");
                        }
                    }
                    break;
                case "R6645x_load_Normal1_gamma":
                    {
                        string strLine = Convert.ToString(R66455_txtRegData.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组
                        int cnt2 = 1;

                        if (arr.Length == 0x92)
                        {
                            for (int n = 0; n < 79; n++)
                            {
                                if (n == 0 || n == 1 || n == 27 || n == 53)
                                {
                                    Measured_Registers[n] = 0;
                                }
                                else if (n == 25 || n == 51 || n == 77)
                                {
                                    Measured_Registers[n] = 0x3ff;
                                }
                                else
                                {
                                    Measured_Registers[n] = (byte.Parse((Convert.ToInt32(arr[cnt2], 16)).ToString()) << 8) + byte.Parse((Convert.ToInt32(arr[cnt2 + 1], 16)).ToString());
                                    cnt2 = cnt2 + 2;
                                }
                            }
                            R6645x_load_Normal1_gamma.BackColor = Color.Green;
                            MessageBox.Show("Gamma值载入ok!", "系统提示：");
                        }
                        else
                        {
                            MessageBox.Show("请导入正确的Gamma值!", "系统提示：");
                        }
                    }
                    break;

                default: break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void R6645x_Measure_Information()//double Dgv_lum, double Dgv_x, double Dgv_y
        {
            byte[] IR_Drop_Gray_table = new byte[17] { 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 255 };
            string[] IR_Drop_Area_table = new string[9] { "上左", "上中", "上右", "中左", "中中", "中右", "下左", "下中", "下右" };

            //------------------------------------position 9------------------------------------
            if (R6645x_tabControl.SelectedIndex == 0)
            {
                int Row_Accum0 = 0, Current_Index0 = 0, Row_flag0 = 0;
                DataGridViewRow Row0 = new DataGridViewRow();
                Row_Accum0 = this.R6645x_dataGridView0.Rows.Count - 1;

                frmHelp.numPatternR.Value = 255; frmHelp.numPatternG.Value = 255; frmHelp.numPatternB.Value = 255;


                if (Row_Accum0 != 0)
                {
                    Current_Index0 = this.R6645x_dataGridView0.SelectedCells[0].RowIndex;
                    if ((Current_Index0 != Row_Accum0 - 1) && (Current_Index0 != Row_Accum0))
                    {
                        Row_Accum0 = Current_Index0;
                        Row_flag0 = 1;
                    }
                    else
                    {
                        if (Row_Accum0 < 9)
                        {
                            this.R6645x_dataGridView0.Rows.Add(Row0);
                        }
                        else
                        {
                            goto R6645x_end;
                        }
                    }
                    if ((Row_Accum0 != 0) && (Row_flag0 != 1))
                    {
                        if (Current_Index0 != Row_Accum0)
                        {
                            this.R6645x_dataGridView0.Rows[Row_Accum0 - 1].Selected = false;
                            this.R6645x_dataGridView0.Rows[Row_Accum0].Selected = true;
                        }
                    }
                }
                else //第一笔
                {
                    this.R6645x_dataGridView0.Rows.Add(Row0);
                }
                if (r6645x_cbo_index.SelectedIndex == 0)//r66451
                {
                    R66451_btnPrevPic_Run("P" + frmHelp.numPatternR.Value.ToString() + frmHelp.numPatternG.Value.ToString() + frmHelp.numPatternB.Value.ToString() + ".dsc_0001.bmp");
                }
                else
                {
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                }
                Thread.Sleep(200);
                frmHelp.Msr_CASDK();

                this.R6645x_dataGridView0.Rows[Row_Accum0].Cells[0].Value = Row_Accum0 + 1;//序号
                this.R6645x_dataGridView0.Rows[Row_Accum0].Cells[1].Value = IR_Drop_Area_table[Row_Accum0];//灰阶
                                                                                                           //Rows[x]为第x行,Cells[x]为第x列
                this.R6645x_dataGridView0.Rows[Row_Accum0].Cells[2].Value = frmHelp.CASDK_x.ToString("#0.0000");
                this.R6645x_dataGridView0.Rows[Row_Accum0].Cells[3].Value = frmHelp.CASDK_y.ToString("#0.0000");
                this.R6645x_dataGridView0.Rows[Row_Accum0].Cells[4].Value = frmHelp.CASDK_LUM.ToString("#0.0000");

                MeasDataOf9Pos[Row_Accum0, 0] = frmHelp.CASDK_x;
                MeasDataOf9Pos[Row_Accum0, 1] = frmHelp.CASDK_y;
                MeasDataOf9Pos[Row_Accum0, 2] = frmHelp.CASDK_LUM;

                if (Row_flag0 == 1)
                {
                    Row_flag0 = 0;
                    Row_Accum0 = this.R6645x_dataGridView0.Rows.Count - 1;
                    this.R6645x_dataGridView0.Rows[Current_Index0].Selected = false;
                    this.R6645x_dataGridView0.Rows[Row_Accum0 - 1].Selected = true;
                }
                R6645x_dataGridView0.FirstDisplayedScrollingRowIndex = R6645x_dataGridView0.RowCount - 1;
            }
            //-------------------------while----------------------------------
            else if (R6645x_tabControl.SelectedIndex == 1)
            {
                int Row_Accum1 = 0, Current_Index1 = 0, Row_flag1 = 0;
                DataGridViewRow Row1 = new DataGridViewRow();
                Row_Accum1 = this.R6645x_dataGridView1.Rows.Count - 1;

                if (Row_Accum1 != 0)
                {
                    Current_Index1 = this.R6645x_dataGridView1.SelectedCells[0].RowIndex;
                    if ((Current_Index1 != Row_Accum1 - 1) && (Current_Index1 != Row_Accum1))
                    {
                        Row_Accum1 = Current_Index1;
                        frmHelp.numPatternR.Value = IR_Drop_Gray_table[Current_Index1]; frmHelp.numPatternG.Value = IR_Drop_Gray_table[Current_Index1]; frmHelp.numPatternB.Value = IR_Drop_Gray_table[Current_Index1];
                        Row_flag1 = 1;
                    }
                    else
                    {
                        if (Row_Accum1 < 17)
                        {
                            frmHelp.numPatternR.Value = IR_Drop_Gray_table[Current_Index1 + 1]; frmHelp.numPatternG.Value = IR_Drop_Gray_table[Current_Index1 + 1]; frmHelp.numPatternB.Value = IR_Drop_Gray_table[Current_Index1 + 1];
                            this.R6645x_dataGridView1.Rows.Add(Row1);
                        }
                        else
                        {
                            goto R6645x_end;
                        }
                    }

                    if ((Row_Accum1 != 0) && (Row_flag1 != 1))
                    {
                        if (Current_Index1 != Row_Accum1)
                        {
                            this.R6645x_dataGridView1.Rows[Row_Accum1 - 1].Selected = false;
                            this.R6645x_dataGridView1.Rows[Row_Accum1].Selected = true;
                        }
                    }
                }
                else
                {
                    frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = 0;
                    this.R6645x_dataGridView1.Rows.Add(Row1);
                }
                if (r6645x_cbo_index.SelectedIndex == 0)//r66451
                {
                    R66451_btnPrevPic_Run("P" + frmHelp.numPatternR.Value.ToString() + frmHelp.numPatternG.Value.ToString() + frmHelp.numPatternB.Value.ToString() + ".dsc_0001.bmp");
                }
                else
                {
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                }
                Thread.Sleep(200);
                frmHelp.Msr_CASDK();

                this.R6645x_dataGridView1.Rows[Row_Accum1].Cells[0].Value = Row_Accum1 + 1;//序号
                this.R6645x_dataGridView1.Rows[Row_Accum1].Cells[1].Value = IR_Drop_Gray_table[Row_Accum1];//灰阶
                                                                                                           //Rows[x]为第x行,Cells[x]为第x列
                this.R6645x_dataGridView1.Rows[Row_Accum1].Cells[2].Value = frmHelp.CASDK_x.ToString("#0.0000");
                this.R6645x_dataGridView1.Rows[Row_Accum1].Cells[3].Value = frmHelp.CASDK_y.ToString("#0.0000");
                this.R6645x_dataGridView1.Rows[Row_Accum1].Cells[4].Value = frmHelp.CASDK_LUM.ToString("#0.0000");

                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum1 * 4 + 0] = frmHelp.CASDK_x;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum1 * 4 + 1] = frmHelp.CASDK_y;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum1 * 4 + 2] = frmHelp.CASDK_LUM;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum1 * 4 + 3] = IR_Drop_Gray_table[Row_Accum1];

                if (Row_flag1 == 1)
                {
                    Row_flag1 = 0;
                    Row_Accum1 = this.R6645x_dataGridView1.Rows.Count - 1;
                    this.R6645x_dataGridView1.Rows[Current_Index1].Selected = false;
                    this.R6645x_dataGridView1.Rows[Row_Accum1 - 1].Selected = true;
                }
                R6645x_dataGridView1.FirstDisplayedScrollingRowIndex = R6645x_dataGridView1.RowCount - 1;
            }
            //-------------------------Red-----------------------------------
            else if (R6645x_tabControl.SelectedIndex == 2)
            {
                int Row_Accum2 = 0, Current_Index2 = 0, Row_flag2 = 0;
                DataGridViewRow Row2 = new DataGridViewRow();
                Row_Accum2 = this.R6645x_dataGridView2.Rows.Count - 1;

                if (Row_Accum2 != 0)
                {
                    Current_Index2 = this.R6645x_dataGridView2.SelectedCells[0].RowIndex;
                    if ((Current_Index2 != Row_Accum2 - 1) && (Current_Index2 != Row_Accum2))
                    {
                        frmHelp.numPatternR.Value = IR_Drop_Gray_table[Current_Index2]; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = 0;
                        Row_Accum2 = Current_Index2;
                        Row_flag2 = 1;
                    }
                    else
                    {
                        if (Row_Accum2 < 17)
                        {
                            frmHelp.numPatternR.Value = IR_Drop_Gray_table[Current_Index2 + 1]; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = 0;
                            this.R6645x_dataGridView2.Rows.Add(Row2);
                        }
                        else
                        {
                            goto R6645x_end;
                        }
                    }
                    if ((Row_Accum2 != 0) && (Row_flag2 != 1))
                    {
                        if (Current_Index2 != Row_Accum2)
                        {
                            this.R6645x_dataGridView2.Rows[Row_Accum2 - 1].Selected = false;
                            this.R6645x_dataGridView2.Rows[Row_Accum2].Selected = true;
                        }
                    }
                }
                else
                {
                    frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = 0;
                    this.R6645x_dataGridView2.Rows.Add(Row2);
                }
                if (r6645x_cbo_index.SelectedIndex == 0)//r66451
                {
                    R66451_btnPrevPic_Run("P" + frmHelp.numPatternR.Value.ToString() + frmHelp.numPatternG.Value.ToString() + frmHelp.numPatternB.Value.ToString() + ".dsc_0001.bmp");
                }
                else
                {
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                }
                Thread.Sleep(200);
                frmHelp.Msr_CASDK();
                this.R6645x_dataGridView2.Rows[Row_Accum2].Cells[0].Value = Row_Accum2 + 1;//序号
                this.R6645x_dataGridView2.Rows[Row_Accum2].Cells[1].Value = IR_Drop_Gray_table[Row_Accum2];//灰阶
                                                                                                           //Rows[x]为第x行,Cells[x]为第x列
                this.R6645x_dataGridView2.Rows[Row_Accum2].Cells[2].Value = frmHelp.CASDK_x.ToString("#0.0000");
                this.R6645x_dataGridView2.Rows[Row_Accum2].Cells[3].Value = frmHelp.CASDK_y.ToString("#0.0000");
                this.R6645x_dataGridView2.Rows[Row_Accum2].Cells[4].Value = frmHelp.CASDK_LUM.ToString("#0.0000");

                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum2 * 4 + 0] = frmHelp.CASDK_x;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum2 * 4 + 1] = frmHelp.CASDK_y;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum2 * 4 + 2] = frmHelp.CASDK_LUM;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum2 * 4 + 3] = IR_Drop_Gray_table[Row_Accum2];

                if (Row_flag2 == 1)
                {
                    Row_flag2 = 0;
                    Row_Accum2 = this.R6645x_dataGridView2.Rows.Count - 1;
                    this.R6645x_dataGridView2.Rows[Current_Index2].Selected = false;
                    this.R6645x_dataGridView2.Rows[Row_Accum2 - 1].Selected = true;
                }
                R6645x_dataGridView2.FirstDisplayedScrollingRowIndex = R6645x_dataGridView2.RowCount - 1;
            }
            //-------------------------Green-----------------------------------
            else if (R6645x_tabControl.SelectedIndex == 3)
            {
                int Row_Accum3 = 0, Current_Index3 = 0, Row_flag3 = 0;
                DataGridViewRow Row3 = new DataGridViewRow();
                Row_Accum3 = this.R6645x_dataGridView3.Rows.Count - 1;

                if (Row_Accum3 != 0)
                {
                    Current_Index3 = this.R6645x_dataGridView3.SelectedCells[0].RowIndex;
                    if ((Current_Index3 != Row_Accum3 - 1) && (Current_Index3 != Row_Accum3))
                    {
                        frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = IR_Drop_Gray_table[Current_Index3]; frmHelp.numPatternB.Value = 0;
                        Row_Accum3 = Current_Index3;
                        Row_flag3 = 1;
                    }
                    else
                    {
                        if (Row_Accum3 < 17)
                        {
                            frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = IR_Drop_Gray_table[Current_Index3 + 1]; frmHelp.numPatternB.Value = 0;

                            this.R6645x_dataGridView3.Rows.Add(Row3);
                        }
                        else
                        {
                            goto R6645x_end;
                        }
                    }
                    if ((Row_Accum3 != 0) && (Row_flag3 != 1))
                    {
                        if (Current_Index3 != Row_Accum3)
                        {
                            this.R6645x_dataGridView3.Rows[Row_Accum3 - 1].Selected = false;
                            this.R6645x_dataGridView3.Rows[Row_Accum3].Selected = true;
                        }
                    }
                }
                else
                {
                    frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = 0;
                    this.R6645x_dataGridView3.Rows.Add(Row3);
                }
                if (r6645x_cbo_index.SelectedIndex == 0)//R66451
                {
                    R66451_btnPrevPic_Run("P" + frmHelp.numPatternR.Value.ToString() + frmHelp.numPatternG.Value.ToString() + frmHelp.numPatternB.Value.ToString() + ".dsc_0001.bmp");
                }
                else
                {
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                }
                Thread.Sleep(200);
                frmHelp.Msr_CASDK();
                this.R6645x_dataGridView3.Rows[Row_Accum3].Cells[0].Value = Row_Accum3 + 1;//序号
                this.R6645x_dataGridView3.Rows[Row_Accum3].Cells[1].Value = IR_Drop_Gray_table[Row_Accum3];//灰阶
                                                                                                           //Rows[x]为第x行,Cells[x]为第x列
                this.R6645x_dataGridView3.Rows[Row_Accum3].Cells[2].Value = frmHelp.CASDK_x.ToString("#0.0000");
                this.R6645x_dataGridView3.Rows[Row_Accum3].Cells[3].Value = frmHelp.CASDK_y.ToString("#0.0000");
                this.R6645x_dataGridView3.Rows[Row_Accum3].Cells[4].Value = frmHelp.CASDK_LUM.ToString("#0.0000");

                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum3 * 4 + 0] = frmHelp.CASDK_x;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum3 * 4 + 1] = frmHelp.CASDK_y;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum3 * 4 + 2] = frmHelp.CASDK_LUM;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum3 * 4 + 3] = IR_Drop_Gray_table[Row_Accum3];

                if (Row_flag3 == 1)
                {
                    Row_flag3 = 0;
                    Row_Accum3 = this.R6645x_dataGridView3.Rows.Count - 1;
                    this.R6645x_dataGridView3.Rows[Current_Index3].Selected = false;
                    this.R6645x_dataGridView3.Rows[Row_Accum3 - 1].Selected = true;
                }
                R6645x_dataGridView3.FirstDisplayedScrollingRowIndex = R6645x_dataGridView3.RowCount - 1;
            }
            //-------------------------blue-----------------------------------
            else if (R6645x_tabControl.SelectedIndex == 4)
            {
                int Row_Accum4 = 0, Current_Index4 = 0, Row_flag4 = 0;
                DataGridViewRow Row4 = new DataGridViewRow();
                Row_Accum4 = this.R6645x_dataGridView4.Rows.Count - 1;

                if (Row_Accum4 != 0)
                {
                    Current_Index4 = this.R6645x_dataGridView4.SelectedCells[0].RowIndex;

                    if ((Current_Index4 != Row_Accum4 - 1) && (Current_Index4 != Row_Accum4))
                    {
                        frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = IR_Drop_Gray_table[Current_Index4];
                        Row_Accum4 = Current_Index4;
                        Row_flag4 = 1;
                    }
                    else
                    {
                        if (Row_Accum4 < 17)
                        {
                            frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = IR_Drop_Gray_table[Current_Index4 + 1];
                            this.R6645x_dataGridView4.Rows.Add(Row4);
                        }
                        else
                        {
                            goto R6645x_end;
                        }
                    }
                    if ((Row_Accum4 != 0) && (Row_flag4 != 1))
                    {
                        if (Current_Index4 != Row_Accum4)
                        {
                            this.R6645x_dataGridView4.Rows[Row_Accum4 - 1].Selected = false;
                            this.R6645x_dataGridView4.Rows[Row_Accum4].Selected = true;
                        }
                    }
                }
                else //首行
                {
                    frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = 0;
                    this.R6645x_dataGridView4.Rows.Add(Row4);
                }
                if (r6645x_cbo_index.SelectedIndex == 0)
                {
                    R66451_btnPrevPic_Run("P" + frmHelp.numPatternR.Value.ToString() + frmHelp.numPatternG.Value.ToString() + frmHelp.numPatternB.Value.ToString() + ".dsc_0001.bmp");
                }
                else
                {
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                }
                Thread.Sleep(200);
                frmHelp.Msr_CASDK();
                this.R6645x_dataGridView4.Rows[Row_Accum4].Cells[0].Value = Row_Accum4 + 1;//序号
                this.R6645x_dataGridView4.Rows[Row_Accum4].Cells[1].Value = IR_Drop_Gray_table[Row_Accum4];//灰阶
                                                                                                           //Rows[x]为第x行,Cells[x]为第x列
                this.R6645x_dataGridView4.Rows[Row_Accum4].Cells[2].Value = frmHelp.CASDK_x.ToString("#0.0000");
                this.R6645x_dataGridView4.Rows[Row_Accum4].Cells[3].Value = frmHelp.CASDK_y.ToString("#0.0000");
                this.R6645x_dataGridView4.Rows[Row_Accum4].Cells[4].Value = frmHelp.CASDK_LUM.ToString("#0.0000");

                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum4 * 4 + 0] = frmHelp.CASDK_x;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum4 * 4 + 1] = frmHelp.CASDK_y;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum4 * 4 + 2] = frmHelp.CASDK_LUM;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum4 * 4 + 3] = IR_Drop_Gray_table[Row_Accum4];

                if (Row_flag4 == 1)
                {
                    Row_flag4 = 0;
                    Row_Accum4 = this.R6645x_dataGridView4.Rows.Count - 1;
                    this.R6645x_dataGridView4.Rows[Current_Index4].Selected = false;
                    this.R6645x_dataGridView4.Rows[Row_Accum4 - 1].Selected = true;
                }
                R6645x_dataGridView4.FirstDisplayedScrollingRowIndex = R6645x_dataGridView4.RowCount - 1;
            }
            //-------------------------cyan 青-----------------------------------
            else if (R6645x_tabControl.SelectedIndex == 5)
            {
                int Row_Accum5 = 0, Current_Index5 = 0, Row_flag5 = 0;
                DataGridViewRow Row5 = new DataGridViewRow();
                Row_Accum5 = this.R6645x_dataGridView5.Rows.Count - 1;

                if (Row_Accum5 != 0)
                {
                    Current_Index5 = this.R6645x_dataGridView5.SelectedCells[0].RowIndex;
                    if ((Current_Index5 != Row_Accum5 - 1) && (Current_Index5 != Row_Accum5))
                    {
                        frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = IR_Drop_Gray_table[Current_Index5]; frmHelp.numPatternB.Value = IR_Drop_Gray_table[Current_Index5];
                        Row_Accum5 = Current_Index5;
                        Row_flag5 = 1;
                    }
                    else
                    {
                        if (Row_Accum5 < 17)
                        {
                            frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = IR_Drop_Gray_table[Current_Index5 + 1]; frmHelp.numPatternB.Value = IR_Drop_Gray_table[Current_Index5 + 1];
                            this.R6645x_dataGridView5.Rows.Add(Row5);
                        }
                        else
                        {
                            goto R6645x_end;
                        }
                    }
                    if ((Row_Accum5 != 0) && (Row_flag5 != 1))
                    {
                        if (Current_Index5 != Row_Accum5)
                        {
                            this.R6645x_dataGridView5.Rows[Row_Accum5 - 1].Selected = false;
                            this.R6645x_dataGridView5.Rows[Row_Accum5].Selected = true;
                        }
                    }
                }
                else //首行
                {
                    frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = 0;
                    this.R6645x_dataGridView5.Rows.Add(Row5);
                }
                if (r6645x_cbo_index.SelectedIndex == 0)//r66451
                {
                    R66451_btnPrevPic_Run("P" + frmHelp.numPatternR.Value.ToString() + frmHelp.numPatternG.Value.ToString() + frmHelp.numPatternB.Value.ToString() + ".dsc_0001.bmp");
                }
                else
                {
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                }
                Thread.Sleep(200);
                frmHelp.Msr_CASDK();
                this.R6645x_dataGridView5.Rows[Row_Accum5].Cells[0].Value = Row_Accum5 + 1;//序号
                this.R6645x_dataGridView5.Rows[Row_Accum5].Cells[1].Value = IR_Drop_Gray_table[Row_Accum5];//灰阶
                                                                                                           //Rows[x]为第x行,Cells[x]为第x列

                this.R6645x_dataGridView5.Rows[Row_Accum5].Cells[2].Value = frmHelp.CASDK_x.ToString("#0.0000");
                this.R6645x_dataGridView5.Rows[Row_Accum5].Cells[3].Value = frmHelp.CASDK_y.ToString("#0.0000");
                this.R6645x_dataGridView5.Rows[Row_Accum5].Cells[4].Value = frmHelp.CASDK_LUM.ToString("#0.0000");

                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum5 * 4 + 0] = frmHelp.CASDK_x;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum5 * 4 + 1] = frmHelp.CASDK_y;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum5 * 4 + 2] = frmHelp.CASDK_LUM;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum5 * 4 + 3] = IR_Drop_Gray_table[Row_Accum5];

                if (Row_flag5 == 1)
                {
                    Row_flag5 = 0;
                    Row_Accum5 = this.R6645x_dataGridView5.Rows.Count - 1;
                    this.R6645x_dataGridView5.Rows[Current_Index5].Selected = false;
                    this.R6645x_dataGridView5.Rows[Row_Accum5 - 1].Selected = true;
                }
                R6645x_dataGridView5.FirstDisplayedScrollingRowIndex = R6645x_dataGridView5.RowCount - 1;
            }
            //-------------------------magenta 紫-----------------------------------
            else if (R6645x_tabControl.SelectedIndex == 6)
            {
                int Row_Accum6 = 0, Current_Index6 = 0, Row_flag6 = 0;
                DataGridViewRow Row6 = new DataGridViewRow();
                Row_Accum6 = this.R6645x_dataGridView6.Rows.Count - 1;

                if (Row_Accum6 != 0)
                {
                    Current_Index6 = this.R6645x_dataGridView6.SelectedCells[0].RowIndex;

                    if ((Current_Index6 != Row_Accum6 - 1) && (Current_Index6 != Row_Accum6))
                    {
                        frmHelp.numPatternR.Value = IR_Drop_Gray_table[Current_Index6]; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = IR_Drop_Gray_table[Current_Index6];
                        Row_Accum6 = Current_Index6;
                        Row_flag6 = 1;
                    }
                    else
                    {
                        if (Row_Accum6 < 17)
                        {
                            frmHelp.numPatternR.Value = IR_Drop_Gray_table[Current_Index6 + 1]; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = IR_Drop_Gray_table[Current_Index6 + 1];
                            this.R6645x_dataGridView6.Rows.Add(Row6);
                        }
                        else
                        {
                            goto R6645x_end;
                        }
                    }
                    if ((Row_Accum6 != 0) && (Row_flag6 != 1))
                    {
                        if (Current_Index6 != Row_Accum6)
                        {
                            this.R6645x_dataGridView6.Rows[Row_Accum6 - 1].Selected = false;
                            this.R6645x_dataGridView6.Rows[Row_Accum6].Selected = true;
                        }
                    }
                }
                else //首行
                {
                    frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = 0;
                    this.R6645x_dataGridView6.Rows.Add(Row6);
                }
                if (r6645x_cbo_index.SelectedIndex == 0)//R66451
                {
                    R66451_btnPrevPic_Run("P" + frmHelp.numPatternR.Value.ToString() + frmHelp.numPatternG.Value.ToString() + frmHelp.numPatternB.Value.ToString() + ".dsc_0001.bmp");
                }
                else
                {
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                }
                Thread.Sleep(200);
                frmHelp.Msr_CASDK();
                this.R6645x_dataGridView6.Rows[Row_Accum6].Cells[0].Value = Row_Accum6 + 1;//序号
                this.R6645x_dataGridView6.Rows[Row_Accum6].Cells[1].Value = IR_Drop_Gray_table[Row_Accum6];//灰阶
                                                                                                           //Rows[x]为第x行,Cells[x]为第x列
                this.R6645x_dataGridView6.Rows[Row_Accum6].Cells[2].Value = frmHelp.CASDK_x.ToString("#0.0000");
                this.R6645x_dataGridView6.Rows[Row_Accum6].Cells[3].Value = frmHelp.CASDK_y.ToString("#0.0000");
                this.R6645x_dataGridView6.Rows[Row_Accum6].Cells[4].Value = frmHelp.CASDK_LUM.ToString("#0.0000");

                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum6 * 4 + 0] = frmHelp.CASDK_x;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum6 * 4 + 1] = frmHelp.CASDK_y;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum6 * 4 + 2] = frmHelp.CASDK_LUM;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum6 * 4 + 3] = IR_Drop_Gray_table[Row_Accum6];

                if (Row_flag6 == 1)
                {
                    Row_flag6 = 0;
                    Row_Accum6 = this.R6645x_dataGridView6.Rows.Count - 1;
                    this.R6645x_dataGridView6.Rows[Current_Index6].Selected = false;
                    this.R6645x_dataGridView6.Rows[Row_Accum6 - 1].Selected = true;
                }
                R6645x_dataGridView6.FirstDisplayedScrollingRowIndex = R6645x_dataGridView6.RowCount - 1;
            }
            //------------------------------------yellow------------------------------------
            else if (R6645x_tabControl.SelectedIndex == 7)
            {
                int Row_Accum7 = 0, Current_Index7 = 0, Row_flag7 = 0;
                DataGridViewRow Row7 = new DataGridViewRow();
                Row_Accum7 = this.R6645x_dataGridView.Rows.Count - 1;

                if (Row_Accum7 != 0)
                {
                    Current_Index7 = this.R6645x_dataGridView.SelectedCells[0].RowIndex;
                    if ((Current_Index7 != Row_Accum7 - 1) && (Current_Index7 != Row_Accum7))
                    {
                        frmHelp.numPatternR.Value = IR_Drop_Gray_table[Current_Index7]; frmHelp.numPatternG.Value = IR_Drop_Gray_table[Current_Index7]; frmHelp.numPatternB.Value = 0;
                        Row_Accum7 = Current_Index7;
                        Row_flag7 = 1; //流水
                    }
                    else
                    {
                        if (Row_Accum7 < 17)
                        {
                            frmHelp.numPatternR.Value = IR_Drop_Gray_table[Current_Index7 + 1]; frmHelp.numPatternG.Value = IR_Drop_Gray_table[Current_Index7 + 1]; frmHelp.numPatternB.Value = 0;
                            this.R6645x_dataGridView.Rows.Add(Row7);
                        }
                        else
                        {
                            goto R6645x_end;
                        }
                    }
                    if ((Row_Accum7 != 0) && (Row_flag7 != 1)) //回调
                    {
                        if (Current_Index7 != Row_Accum7)
                        {
                            this.R6645x_dataGridView.Rows[Row_Accum7 - 1].Selected = false;
                            this.R6645x_dataGridView.Rows[Row_Accum7].Selected = true;
                        }
                    }
                }
                else //首行
                {
                    frmHelp.numPatternR.Value = 0; frmHelp.numPatternG.Value = 0; frmHelp.numPatternB.Value = 0;
                    this.R6645x_dataGridView.Rows.Add(Row7);
                }
                if (r6645x_cbo_index.SelectedIndex == 0) //r66451
                {
                    R66451_btnPrevPic_Run("P" + frmHelp.numPatternR.Value.ToString() + frmHelp.numPatternG.Value.ToString() + frmHelp.numPatternB.Value.ToString() + ".dsc_0001.bmp");
                }
                else
                {
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                }
                Thread.Sleep(200);
                frmHelp.Msr_CASDK();
                this.R6645x_dataGridView.Rows[Row_Accum7].Cells[0].Value = Row_Accum7 + 1;//序号
                this.R6645x_dataGridView.Rows[Row_Accum7].Cells[1].Value = IR_Drop_Gray_table[Row_Accum7];//灰阶
                                                                                                          //Rows[x]为第x行,Cells[x]为第x列
                this.R6645x_dataGridView.Rows[Row_Accum7].Cells[2].Value = frmHelp.CASDK_x.ToString("#0.0000");
                this.R6645x_dataGridView.Rows[Row_Accum7].Cells[3].Value = frmHelp.CASDK_y.ToString("#0.0000");
                this.R6645x_dataGridView.Rows[Row_Accum7].Cells[4].Value = frmHelp.CASDK_LUM.ToString("#0.0000");

                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum7 * 4 + 0] = frmHelp.CASDK_x;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum7 * 4 + 1] = frmHelp.CASDK_y;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum7 * 4 + 2] = frmHelp.CASDK_LUM;
                MeasDataOfGray[R6645x_tabControl.SelectedIndex - 1, Row_Accum7 * 4 + 3] = IR_Drop_Gray_table[Row_Accum7];

                if (Row_flag7 == 1)
                {
                    Row_flag7 = 0;
                    Row_Accum7 = this.R6645x_dataGridView.Rows.Count - 1;
                    this.R6645x_dataGridView.Rows[Current_Index7].Selected = false;
                    this.R6645x_dataGridView.Rows[Row_Accum7 - 1].Selected = true;
                }
                R6645x_dataGridView.FirstDisplayedScrollingRowIndex = R6645x_dataGridView.RowCount - 1;
            }
        R6645x_end:;
        }
        private void R66451_CheckedChanged(object sender, EventArgs e)
        {

        }
        private void R6645x_auto_gamma(Object myObject, EventArgs myEventArgs)
        {
            if (Gamma_Auto_flag_dll == 1) //use the gamma dll
            {
                R6645x_dll_gamma();
            }
            else if (Gamma_Auto_flag_dll == 0)
            {
                R6645x_1D_gamma();
            }
        }

        private void R6645x_dll_gamma()
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//排除0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((frmHelp.CASDK_LUM < (D_lum[index] + lum_off)) && (frmHelp.CASDK_LUM > (D_lum[index] - lum_off)))//G
                {
                    flag1 = 0;
                }
                else
                {
                    flag1 = 1;
                }
                if ((frmHelp.CASDK_y < (D_y + y_off)) && (frmHelp.CASDK_y > (D_y - y_off)) && flag1 == 0)//B
                    flag2 = 0;
                if ((frmHelp.CASDK_x < (D_x + x_off)) && (frmHelp.CASDK_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
                    flag3 = 0;
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;

                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;

                R66455_GAMMA_GRAY[index] = Target_Level[index];
                R66455_CA310_LUM[index] = frmHelp.CASDK_LUM;
                R66455_CA310_X[index] = frmHelp.CASDK_x;
                R66455_CA310_Y[index] = frmHelp.CASDK_y;

                Last_reg_red[index] = R66455_RED.Value;
                Last_reg_green[index] = R66455_GREEN.Value;
                Last_reg_blue[index] = R66455_BLUE.Value;

                index++;
                if (index == Parttern_R66455.Length)   //VVV
                {
                    R66455_GAMMA_GRAY[index] = Target_Level[index];
                    R66455_CA310_LUM[index] = 0;
                    R66455_CA310_X[index] = 0;
                    R66455_CA310_Y[index] = 0;

                    frmHelp.numPatternR.Value = 0;
                    frmHelp.numPatternG.Value = 0;
                    frmHelp.numPatternB.Value = 0;
                    if (r6645x_cbo_index.SelectedIndex == 0)//R66451
                    {
                        if (rBt_R66455_normal5_gamma.Checked == true)
                        {
                            R66451_btnPrevPic_Run("A000.dsc_0001.bmp");
                        }
                        else
                        { R66451_btnPrevPic_Run("P000.dsc_0001.bmp"); }

                    }
                    else
                    {
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);
                    }//写入Pattern参数\

                    R66455_Gary.Value = 0;
                    R66455_RED.Value = 0;
                    R66455_GREEN.Value = 0;
                    R66455_BLUE.Value = 0;
                    R66455_GammaW_Click(R66455_GammaW, null);

                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "R66455 gamma调试完毕!\r\n";
                }
                if (index <= Parttern_R66455.Length - 1) //绑点切换部分   //VVV
                {
                    switch (index)
                    {
                        case 1:
                            lum_off = (Convert.ToDouble(textR66455_252.Text) * Convert.ToDouble(textR66455_252_off.Text)) / 100;
                            break;
                        case 2:
                            lum_off = (Convert.ToDouble(textR66455_244.Text) * Convert.ToDouble(textR66455_244_off.Text)) / 100;
                            break;
                        case 3:
                            lum_off = (Convert.ToDouble(textR66455_236.Text) * Convert.ToDouble(textR66455_236_off.Text)) / 100;
                            break;
                        case 4:
                            lum_off = (Convert.ToDouble(textR66455_228.Text) * Convert.ToDouble(textR66455_228_off.Text)) / 100;
                            break;
                        case 5:
                            lum_off = (Convert.ToDouble(textR66455_220.Text) * Convert.ToDouble(textR66455_220_off.Text)) / 100;
                            break;
                        case 6:
                            lum_off = (Convert.ToDouble(textR66455_212.Text) * Convert.ToDouble(textR66455_212_off.Text)) / 100;
                            break;
                        case 7:
                            lum_off = (Convert.ToDouble(textR66455_196.Text) * Convert.ToDouble(textR66455_196_off.Text)) / 100;
                            break;
                        case 8:
                            lum_off = (Convert.ToDouble(textR66455_188.Text) * Convert.ToDouble(textR66455_188_off.Text)) / 100;
                            break;
                        case 9:
                            lum_off = (Convert.ToDouble(textR66455_180.Text) * Convert.ToDouble(textR66455_180_off.Text)) / 100;
                            break;
                        case 10:
                            lum_off = (Convert.ToDouble(textR66455_164.Text) * Convert.ToDouble(textR66455_164_off.Text)) / 100;
                            break;
                        case 11:
                            lum_off = (Convert.ToDouble(textR66455_148.Text) * Convert.ToDouble(textR66455_148_off.Text)) / 100;
                            break;
                        case 12:
                            lum_off = (Convert.ToDouble(textR66455_132.Text) * Convert.ToDouble(textR66455_132_off.Text)) / 100;
                            break;
                        case 13:
                            lum_off = (Convert.ToDouble(textR66455_116.Text) * Convert.ToDouble(textR66455_116_off.Text)) / 100;
                            break;
                        case 14:
                            lum_off = (Convert.ToDouble(textR66455_100.Text) * Convert.ToDouble(textR66455_100_off.Text)) / 100;
                            break;
                        case 15:
                            lum_off = (Convert.ToDouble(textR66455_84.Text) * Convert.ToDouble(textR66455_84_off.Text)) / 100;
                            break;
                        case 16:
                            lum_off = (Convert.ToDouble(textR66455_68.Text) * Convert.ToDouble(textR66455_68_off.Text)) / 100;
                            break;
                        case 17:
                            lum_off = (Convert.ToDouble(textR66455_52.Text) * Convert.ToDouble(textR66455_52_off.Text)) / 100;
                            break;
                        case 18:
                            lum_off = (Convert.ToDouble(textR66455_44.Text) * Convert.ToDouble(textR66455_44_off.Text)) / 100;
                            break;
                        case 19:
                            lum_off = (Convert.ToDouble(textR66455_36.Text) * Convert.ToDouble(textR66455_36_off.Text)) / 100;
                            break;
                        case 20:
                            lum_off = (Convert.ToDouble(textR66455_28.Text) * Convert.ToDouble(textR66455_28_off.Text)) / 100;
                            break;
                        case 21:
                            lum_off = (Convert.ToDouble(textR66455_20.Text) * Convert.ToDouble(textR66455_20_off.Text)) / 100;
                            break;
                        case 22:
                            {
                                lum_off = (Convert.ToDouble(textR66455_16.Text) * Convert.ToDouble(textR66455_16_off.Text)) / 100;
                            }
                            break;
                        case 23:
                            {
                                lum_off = (Convert.ToDouble(textR66455_12.Text) * Convert.ToDouble(textR66455_12_off.Text)) / 100;
                            }
                            break;
                        case 24:
                            {
                                lum_off = (Convert.ToDouble(textR66455_10.Text) * Convert.ToDouble(textR66455_10_off.Text)) / 100;
                            }
                            break;
                        case 25:
                            {
                                lum_off = (Convert.ToDouble(textR66455_8.Text) * Convert.ToDouble(textR66455_8_off.Text)) / 100;
                            }
                            break;
                        case 26:
                            {
                                lum_off = (Convert.ToDouble(textR66455_6.Text) * Convert.ToDouble(textR66455_6_off.Text)) / 100;
                            }
                            break;
                        case 27:
                            {
                                lum_off = (Convert.ToDouble(textR66455_4.Text) * Convert.ToDouble(textR66455_4_off.Text)) / 100;
                            }
                            break;
                        case 28:
                            {
                                lum_off = (Convert.ToDouble(textR66455_3.Text) * Convert.ToDouble(textR66455_3_off.Text)) / 100;
                            }
                            break;
                        case 29:
                            {
                                lum_off = (Convert.ToDouble(textR66455_2.Text) * Convert.ToDouble(textR66455_2_off.Text)) / 100;
                            }
                            break;
                    }
                    frmHelp.numPatternR.Value = Parttern_R66455[index];
                    frmHelp.numPatternG.Value = Parttern_R66455[index];
                    frmHelp.numPatternB.Value = Parttern_R66455[index];

                    if (r6645x_cbo_index.SelectedIndex == 0)//r66451
                    {
                        if (rBt_R66455_normal5_gamma.Checked == true)
                        {
                            R66451_btnPrevPic_Run("A" + Parttern_R66455[index].ToString() + Parttern_R66455[index].ToString() + Parttern_R66455[index].ToString() + ".dsc_0001.bmp");
                        }
                        else
                        {
                            R66451_btnPrevPic_Run("P" + Parttern_R66455[index].ToString() + Parttern_R66455[index].ToString() + Parttern_R66455[index].ToString() + ".dsc_0001.bmp");
                        }
                    }
                    else
                    {
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                    }
                    Thread.Sleep(500); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else  //调整部分
            {
                decimal r = 0, g = 0, b = 0;
                int flexible_gamma_count = 120;

                CaSDKObj.YUV_to_RGB(frmHelp.CASDK_y, frmHelp.CASDK_x, out Lr_t, out Lb_t, out Lg_t);

                lumr = frmHelp.CASDK_LUM * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = frmHelp.CASDK_LUM * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = frmHelp.CASDK_LUM * Lb_t / (Lr_t + Lg_t + Lb_t);
                R66455_Gary.Value = Parttern_R66455.Length - index;
                try
                {
                    if (index == 0 || index == 1)
                    {
                        frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R66455_RED.Value, R66455_GREEN.Value, R66455_BLUE.Value, 255, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                    }
                    else
                    {
                        frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R66455_RED.Value, R66455_GREEN.Value, R66455_BLUE.Value, Parttern_R66455[index - 2], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                    }
                    if (Gamma_adjust_timeout_flag == true)
                    {
                        flag1 = 0; flag3 = 0; flag2 = 0;
                    }
                    R66455_RED.Value = r;
                    R66455_GREEN.Value = g;
                    R66455_BLUE.Value = b;

                    R66455_GammaW_Click(R66455_GammaW, null);
                    Thread.Sleep(15);
                    //R66455_gamma_adjust(R66455_GammaW, Parttern_R66455.Length - index, lumr, lumg, lumb); // //VVV
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("数值超限！");
                }
            }
        }
        private void R6645x_1D_gamma()
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//排除0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((frmHelp.CASDK_LUM < (D_lum[index] + lum_off)) && (frmHelp.CASDK_LUM > (D_lum[index] - lum_off)))//G
                {
                    flag1 = 0;
                }
                else
                {
                    flag1 = 1;
                }
                if ((frmHelp.CASDK_y < (D_y + y_off)) && (frmHelp.CASDK_y > (D_y - y_off)) && flag1 == 0)//B
                    flag2 = 0;
                if ((frmHelp.CASDK_x < (D_x + x_off)) && (frmHelp.CASDK_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
                    flag3 = 0;
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;

                index++;
                if (index == 13)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "R6645x 13p gamma调试完毕!\r\n";
                }
                if (index <= 12) //绑点切换部分
                {
                    switch (index)
                    {
                        case 1:
                            lum_off = (Convert.ToDouble(tR6645x_200.Text) * Convert.ToDouble(tR6645x_200_off.Text)) / 100;
                            break;
                        case 2:
                            lum_off = (Convert.ToDouble(tR6645x_128.Text) * Convert.ToDouble(tR6645x_128_off.Text)) / 100;
                            break;
                        case 3:
                            lum_off = (Convert.ToDouble(tR6645x_64.Text) * Convert.ToDouble(tR6645x_64_off.Text)) / 100;
                            break;
                        case 4:
                            lum_off = (Convert.ToDouble(tR6645x_16.Text) * Convert.ToDouble(tR6645x_16_off.Text)) / 100;
                            break;
                        case 5:
                            lum_off = (Convert.ToDouble(tR6645x_5.Text) * Convert.ToDouble(tR6645x_5_off.Text)) / 100;
                            break;
                        case 6:
                            lum_off = (Convert.ToDouble(tR6645x_0.Text) * Convert.ToDouble(tR6645x_0_off.Text)) / 100;
                            break;
                        case 7:
                            lum_off = (Convert.ToDouble(tR6645x_224.Text) * Convert.ToDouble(tR6645x_224_off.Text)) / 100;
                            break;
                        case 8:
                            lum_off = (Convert.ToDouble(tR6645x_160.Text) * Convert.ToDouble(tR6645x_160_off.Text)) / 100;
                            break;
                        case 9:
                            lum_off = (Convert.ToDouble(tR6645x_96.Text) * Convert.ToDouble(tR6645x_96_off.Text)) / 100;
                            break;
                        case 10:
                            lum_off = (Convert.ToDouble(tR6645x_36.Text) * Convert.ToDouble(tR6645x_36_off.Text)) / 100;
                            break;
                        case 11:
                            lum_off = (Convert.ToDouble(tR6645x_9.Text) * Convert.ToDouble(tR6645x_9_off.Text)) / 100;
                            break;
                        case 12:
                            lum_off = (Convert.ToDouble(tR6645x_3.Text) * Convert.ToDouble(tR6645x_3_off.Text)) / 100;
                            break;
                    }
                    frmHelp.numPatternR.Value = Point_R6645x[12 - index];
                    frmHelp.numPatternG.Value = Point_R6645x[12 - index];
                    frmHelp.numPatternB.Value = Point_R6645x[12 - index];
                    if (r6645x_cbo_index.SelectedIndex == 0) //R66451
                    {
                        if (rBt_R66455_normal5_gamma.Checked == true)
                        {
                            R66451_btnPrevPic_Run("A" + Point_R6645x[12 - index].ToString() + Point_R6645x[12 - index].ToString() + Point_R6645x[12 - index].ToString() + ".dsc_0001.bmp");
                        }
                        else
                        {
                            R66451_btnPrevPic_Run("P" + Point_R6645x[12 - index].ToString() + Point_R6645x[12 - index].ToString() + Point_R6645x[12 - index].ToString() + ".dsc_0001.bmp");
                        }
                    }
                    else //r66455
                    {
                        if ((rBt_R66455_normal5_gamma.Checked == true) && (cbx_Nor5_inter_AOD.Checked == true)) //AOD gamma adjust in AOD mode or not
                        {
                            frmHelp.XX0.Value = 250; frmHelp.XX1.Value = 850;//Partial display
                            frmHelp.YY0.Value = 850; frmHelp.YY1.Value = 1500;
                            Application.DoEvents();
                            frmHelp.OLED_Show_Click(frmHelp.btnlogic_pic, null);
                            /*
                            switch (index)
                            {
                                case 1: R66451_btnPrevPic_Run("A" + Point_R6645x[11].ToString() + Point_R6645x[11].ToString() + Point_R6645x[11].ToString() + ".bmp"); break;
                                case 2: R66451_btnPrevPic_Run("A" + Point_R6645x[10].ToString() + Point_R6645x[10].ToString() + Point_R6645x[10].ToString() + ".bmp"); break;
                                case 3: R66451_btnPrevPic_Run("A" + Point_R6645x[9].ToString() + Point_R6645x[9].ToString() + Point_R6645x[9].ToString() + ".bmp"); break;
                                case 4: R66451_btnPrevPic_Run("A" + Point_R6645x[8].ToString() + Point_R6645x[8].ToString() + Point_R6645x[8].ToString() + ".bmp"); break;
                                case 5: R66451_btnPrevPic_Run("A" + Point_R6645x[7].ToString() + Point_R6645x[7].ToString() + Point_R6645x[7].ToString() + ".bmp"); break;
                                case 6: R66451_btnPrevPic_Run("A" + Point_R6645x[6].ToString() + Point_R6645x[6].ToString() + Point_R6645x[6].ToString() + ".bmp"); break;
                                case 7: R66451_btnPrevPic_Run("A" + Point_R6645x[5].ToString() + Point_R6645x[5].ToString() + Point_R6645x[5].ToString() + ".bmp"); break;
                                case 8: R66451_btnPrevPic_Run("A" + Point_R6645x[4].ToString() + Point_R6645x[4].ToString() + Point_R6645x[4].ToString() + ".bmp"); break;
                                case 9: R66451_btnPrevPic_Run("A" + Point_R6645x[3].ToString() + Point_R6645x[3].ToString() + Point_R6645x[3].ToString() + ".bmp"); break;
                                case 10: R66451_btnPrevPic_Run("A" + Point_R6645x[2].ToString() + Point_R6645x[2].ToString() + Point_R6645x[2].ToString() + ".bmp"); break;
                                case 11: R66451_btnPrevPic_Run("A" + Point_R6645x[1].ToString() + Point_R6645x[1].ToString() + Point_R6645x[1].ToString() + ".bmp"); break;
                                case 12: R66451_btnPrevPic_Run("A" + Point_R6645x[0].ToString() + Point_R6645x[0].ToString() + Point_R6645x[0].ToString() + ".bmp"); break;
                                default: break;
                            }*/
                        }
                        else
                        {
                            Application.DoEvents();
                            frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                        }
                    }
                    Thread.Sleep(150); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else  //调整部分
            {
                decimal r, g, b;
                int flexible_gamma_count = 120;

                CaSDKObj.YUV_to_RGB(frmHelp.CASDK_y, frmHelp.CASDK_x, out Lr_t, out Lb_t, out Lg_t);

                lumr = frmHelp.CASDK_LUM * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = frmHelp.CASDK_LUM * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = frmHelp.CASDK_LUM * Lb_t / (Lr_t + Lg_t + Lb_t);
                try
                {
                    switch (index)
                    {
                        case 0: //255
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R6645x_R255.Value, R6645x_G255.Value, R6645x_B255.Value, Point_R6645x[12], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R255.Value = r;
                                R6645x_G255.Value = g;
                                R6645x_B255.Value = b;
                            }
                            break;
                        case 1: //200
                            {
                                frmHelp.Red_Ratio = 0x200;
                                frmHelp.Green_Ratio = 0x200;
                                frmHelp.Blue_Ratio = 0x200;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R6645x_R200.Value, R6645x_G200.Value, R6645x_B200.Value, Point_R6645x[11], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R200.Value = r;
                                R6645x_G200.Value = g;
                                R6645x_B200.Value = b;
                            }
                            break;
                        case 2: //128
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R6645x_R128.Value, R6645x_G128.Value, R6645x_B128.Value, Point_R6645x[10], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R128.Value = r;
                                R6645x_G128.Value = g;
                                R6645x_B128.Value = b;
                            }
                            break;
                        case 3: //64
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R6645x_R64.Value, R6645x_G64.Value, R6645x_B64.Value, Point_R6645x[9], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R64.Value = r;
                                R6645x_G64.Value = g;
                                R6645x_B64.Value = b;
                            }
                            break;
                        case 4: //16
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R6645x_R16.Value, R6645x_G16.Value, R6645x_B16.Value, Point_R6645x[8], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R16.Value = r;
                                R6645x_G16.Value = g;
                                R6645x_B16.Value = b;
                            }
                            break;
                        case 5: //5
                            {
                                frmHelp.Red_Ratio = 0x100;
                                frmHelp.Green_Ratio = 0x100;
                                frmHelp.Blue_Ratio = 0x100;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R6645x_R5.Value, R6645x_G5.Value, R6645x_B5.Value, Point_R6645x[7], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R5.Value = r;
                                R6645x_G5.Value = g;
                                R6645x_B5.Value = b;
                            }
                            break;
                        case 6: //0
                            {
                                Gamma_adjust_timeout_flag = true;
                                R6645x_R0.Value = 0;
                                R6645x_G0.Value = 0;
                                R6645x_B0.Value = 0;
                            }
                            break;
                        case 7: //224 
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R6645x_R224.Value, R6645x_G224.Value, R6645x_B224.Value, Point_R6645x[6], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R224.Value = r;
                                R6645x_G224.Value = g;
                                R6645x_B224.Value = b;
                            }
                            break;
                        case 8: //160 
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R6645x_R160.Value, R6645x_G160.Value, R6645x_B160.Value, Point_R6645x[5], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R160.Value = r;
                                R6645x_G160.Value = g;
                                R6645x_B160.Value = b;
                            }
                            break;
                        case 9: //96
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R6645x_R96.Value, R6645x_G96.Value, R6645x_B96.Value, Point_R6645x[4], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R96.Value = r;
                                R6645x_G96.Value = g;
                                R6645x_B96.Value = b;
                            }
                            break;

                        case 10: //36
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R6645x_R36.Value, R6645x_G36.Value, R6645x_B36.Value, Point_R6645x[3], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R36.Value = r;
                                R6645x_G36.Value = g;
                                R6645x_B36.Value = b;
                            }
                            break;
                        case 11: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], R6645x_R9.Value, R6645x_G9.Value, R6645x_B9.Value, Point_R6645x[2], 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R9.Value = r;
                                R6645x_G9.Value = g;
                                R6645x_B9.Value = b;
                            }
                            break;
                        case 12: //3
                            {
                                /*
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 20, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, R6645x_R3.Value, R6645x_G3.Value, R6645x_B3.Value, Point_R6645x[1], Point_R6645x[7], Point_R6645x[0], R6645x_R16.Value, R6645x_G16.Value, R6645x_B16.Value, R6645x_R9.Value, R6645x_G9.Value, R6645x_B9.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                R6645x_R3.Value = r;
                                R6645x_G3.Value = g;
                                R6645x_B3.Value = b;
                                */

                                Gamma_adjust_timeout_flag = true;
                                R6645x_R3.Value = R6645x_R5.Value - 1;
                                R6645x_G3.Value = R6645x_G5.Value - 1;
                                R6645x_B3.Value = R6645x_B5.Value - 1;
                                Application.DoEvents();

                            }
                            break;
                        default: break;
                    }
                    R6645x_RGammaW_Click(R6645x_RGammaW, null);
                    Thread.Sleep(10);
                    R6645x_RGammaW_Click(R6645x_GGammaW, null);
                    Thread.Sleep(10);
                    R6645x_RGammaW_Click(R6645x_BGammaW, null);
                    Thread.Sleep(10);

                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("数值超限！");
                }
            }
        }

        private int[] Target_Level = new int[31] { 0, 2, 3, 4, 6, 8, 10, 12, 16, 20, 28, 36, 44, 52, 68, 84, 100, 116, 132, 148, 164, 180, 188, 196, 212, 220, 228, 236, 244, 252, 255 };
        private void R66455_Gary_ValueChanged(object sender, EventArgs e)
        {
            R66455_label.Text = Target_Level[Convert.ToByte(R66455_Gary.Value)].ToString();
        }
        private byte[] Point_R6645x = new byte[13] { 3, 9, 36, 96, 160, 224, 0, 5, 16, 64, 128, 200, 255 };
        private void btnCal_R6645x_13Position_Click(object sender, EventArgs e)
        {
            Point_R6645x[0] = Convert.ToByte(nR6645x_p0.Value); //3
            Point_R6645x[1] = Convert.ToByte(nR6645x_p1.Value); //9
            Point_R6645x[2] = Convert.ToByte(nR6645x_p2.Value); //36
            Point_R6645x[3] = Convert.ToByte(nR6645x_p3.Value); //96
            Point_R6645x[4] = Convert.ToByte(nR6645x_p4.Value); //160
            Point_R6645x[5] = Convert.ToByte(nR6645x_p5.Value); //224
            //0
            Point_R6645x[7] = Convert.ToByte(nR6645x_p7.Value); //5
            Point_R6645x[8] = Convert.ToByte(nR6645x_p8.Value); //16
            Point_R6645x[9] = Convert.ToByte(nR6645x_p9.Value); //64
            Point_R6645x[10] = Convert.ToByte(nR6645x_p10.Value);//128
            Point_R6645x[11] = Convert.ToByte(nR6645x_p11.Value);//200

            lb_R6645x_G3.Items[0] = lb_R6645x_G3.Items[0] = lb_R6645x_R3.Items[0] = Point_R6645x[10].ToString();
            lb_R6645x_B3.Items[1] = lb_R6645x_G3.Items[1] = lb_R6645x_R3.Items[1] = Point_R6645x[11].ToString();

            lb_R6645x_B2.Items[0] = lb_R6645x_G2.Items[0] = lb_R6645x_R2.Items[0] = Point_R6645x[5].ToString();
            lb_R6645x_B2.Items[2] = lb_R6645x_G2.Items[2] = lb_R6645x_R2.Items[2] = Point_R6645x[7].ToString();
            lb_R6645x_B2.Items[3] = lb_R6645x_G2.Items[3] = lb_R6645x_R2.Items[3] = Point_R6645x[8].ToString();
            lb_R6645x_B2.Items[4] = lb_R6645x_G2.Items[4] = lb_R6645x_R2.Items[4] = Point_R6645x[9].ToString();

            lb_R6645x_B1.Items[0] = lb_R6645x_G1.Items[0] = lb_R6645x_R1.Items[0] = Point_R6645x[0].ToString();
            lb_R6645x_B1.Items[1] = lb_R6645x_G1.Items[1] = lb_R6645x_R1.Items[1] = Point_R6645x[1].ToString();
            lb_R6645x_B1.Items[2] = lb_R6645x_G1.Items[2] = lb_R6645x_R1.Items[2] = Point_R6645x[2].ToString();
            lb_R6645x_B1.Items[3] = lb_R6645x_G1.Items[3] = lb_R6645x_R1.Items[3] = Point_R6645x[3].ToString();
            lb_R6645x_B1.Items[4] = lb_R6645x_G1.Items[4] = lb_R6645x_R1.Items[4] = Point_R6645x[4].ToString();
            //255

            tR6645x_255.Text = (Math.Pow((Point_R6645x[12] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_200.Text = (Math.Pow((Point_R6645x[11] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_128.Text = (Math.Pow((Point_R6645x[10] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_64.Text = (Math.Pow((Point_R6645x[9] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_16.Text = (Math.Pow((Point_R6645x[8] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_5.Text = (Math.Pow((Point_R6645x[7] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_0.Text = (Math.Pow((Point_R6645x[6] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_224.Text = (Math.Pow((Point_R6645x[5] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_160.Text = (Math.Pow((Point_R6645x[4] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_96.Text = (Math.Pow((Point_R6645x[3] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_36.Text = (Math.Pow((Point_R6645x[2] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_9.Text = (Math.Pow((Point_R6645x[1] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            tR6645x_3.Text = (Math.Pow((Point_R6645x[0] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
        }
        //R66455
        private int[] Parttern_R66455 = new int[30] { 255, 252, 244, 236, 228, 220, 212, 196, 188, 180, 164, 148, 132, 116, 100, 84, 68, 52, 44, 36, 28, 20, 16, 12, 10, 8, 6, 4, 3, 2 };  //VVV
        private void btnCalculate_R66455_Click(object sender, EventArgs e)
        {
            textR66455_255.Text = (Math.Pow((Parttern_R66455[0] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_252.Text = (Math.Pow((Parttern_R66455[1] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_244.Text = (Math.Pow((Parttern_R66455[2] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_236.Text = (Math.Pow((Parttern_R66455[3] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_228.Text = (Math.Pow((Parttern_R66455[4] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_220.Text = (Math.Pow((Parttern_R66455[5] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_212.Text = (Math.Pow((Parttern_R66455[6] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_196.Text = (Math.Pow((Parttern_R66455[7] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_188.Text = (Math.Pow((Parttern_R66455[8] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_180.Text = (Math.Pow((Parttern_R66455[9] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_164.Text = (Math.Pow((Parttern_R66455[10] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_148.Text = (Math.Pow((Parttern_R66455[11] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_132.Text = (Math.Pow((Parttern_R66455[12] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_116.Text = (Math.Pow((Parttern_R66455[13] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_100.Text = (Math.Pow((Parttern_R66455[14] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_84.Text = (Math.Pow((Parttern_R66455[15] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_68.Text = (Math.Pow((Parttern_R66455[16] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_52.Text = (Math.Pow((Parttern_R66455[17] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_44.Text = (Math.Pow((Parttern_R66455[18] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_36.Text = (Math.Pow((Parttern_R66455[19] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_28.Text = (Math.Pow((Parttern_R66455[20] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_20.Text = (Math.Pow((Parttern_R66455[21] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_16.Text = (Math.Pow((Parttern_R66455[22] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_12.Text = (Math.Pow((Parttern_R66455[23] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_10.Text = (Math.Pow((Parttern_R66455[24] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_8.Text = (Math.Pow((Parttern_R66455[25] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_6.Text = (Math.Pow((Parttern_R66455[26] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_4.Text = (Math.Pow((Parttern_R66455[27] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_3.Text = (Math.Pow((Parttern_R66455[28] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");
            textR66455_2.Text = (Math.Pow((Parttern_R66455[29] / 255.0), Convert.ToDouble(txt_R66455_gamma.Text)) * Convert.ToDouble(txt_R66455_lum.Text)).ToString("F4");    //VVV
        }
        private static byte Gamma_Auto_flag_dll = 0;
        private void btnAutoGamma_R66455_Click(object sender, EventArgs e)//255阶为例
        {
            Button btnTmp = (Button)sender;

            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            //------------flexible gamma use------------------
            frmHelp.Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------
            Default_set_Click(null, null);
            switch (btnTmp.Name)
            {
                case "btnAuto_R6645x":
                    {
                        Gamma_Auto_flag_dll = 0; //R6645x_cut_dll
                        D_lums = new double[13, 3];
                        D_lum = new double[13];
                        D_lum[0] = Convert.ToDouble(tR6645x_255.Text);
                        D_lum[1] = Convert.ToDouble(tR6645x_200.Text);
                        D_lum[2] = Convert.ToDouble(tR6645x_128.Text);
                        D_lum[3] = Convert.ToDouble(tR6645x_64.Text);
                        D_lum[4] = Convert.ToDouble(tR6645x_16.Text);
                        D_lum[5] = Convert.ToDouble(tR6645x_5.Text);
                        D_lum[6] = Convert.ToDouble(tR6645x_0.Text);
                        D_lum[7] = Convert.ToDouble(tR6645x_224.Text);
                        D_lum[8] = Convert.ToDouble(tR6645x_160.Text);
                        D_lum[9] = Convert.ToDouble(tR6645x_96.Text);
                        D_lum[10] = Convert.ToDouble(tR6645x_36.Text);
                        D_lum[11] = Convert.ToDouble(tR6645x_9.Text);
                        D_lum[12] = Convert.ToDouble(tR6645x_3.Text);

                        D_x = Convert.ToDouble(txt_R66455_x.Text);
                        D_y = Convert.ToDouble(txt_R66455_y.Text);

                        CaSDKObj.YUV_to_RGB(D_y, D_x, out Lr_d, out Lb_d, out Lg_d);

                        for (int i = 0; i < 13; i++)
                        {
                            D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                            D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                            D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
                        }
                    }
                    break;
                case "btnAutoGammas_R66455":
                    {
                        Gamma_Auto_flag_dll = 1;
                        D_lums = new double[30, 3];  //VVV
                        D_lum = new double[30]; //VVV
                        D_lum[0] = Convert.ToDouble(textR66455_255.Text);
                        D_lum[1] = Convert.ToDouble(textR66455_252.Text);
                        D_lum[2] = Convert.ToDouble(textR66455_244.Text);
                        D_lum[3] = Convert.ToDouble(textR66455_236.Text);
                        D_lum[4] = Convert.ToDouble(textR66455_228.Text);
                        D_lum[5] = Convert.ToDouble(textR66455_220.Text);
                        D_lum[6] = Convert.ToDouble(textR66455_212.Text);
                        D_lum[7] = Convert.ToDouble(textR66455_196.Text);
                        D_lum[8] = Convert.ToDouble(textR66455_188.Text);
                        D_lum[9] = Convert.ToDouble(textR66455_180.Text);
                        D_lum[10] = Convert.ToDouble(textR66455_164.Text);
                        D_lum[11] = Convert.ToDouble(textR66455_148.Text);
                        D_lum[12] = Convert.ToDouble(textR66455_132.Text);
                        D_lum[13] = Convert.ToDouble(textR66455_116.Text);
                        D_lum[14] = Convert.ToDouble(textR66455_100.Text);
                        D_lum[15] = Convert.ToDouble(textR66455_84.Text);
                        D_lum[16] = Convert.ToDouble(textR66455_68.Text);
                        D_lum[17] = Convert.ToDouble(textR66455_52.Text);
                        D_lum[18] = Convert.ToDouble(textR66455_44.Text);
                        D_lum[19] = Convert.ToDouble(textR66455_36.Text);
                        D_lum[20] = Convert.ToDouble(textR66455_28.Text);
                        D_lum[21] = Convert.ToDouble(textR66455_20.Text);
                        D_lum[22] = Convert.ToDouble(textR66455_16.Text);
                        D_lum[23] = Convert.ToDouble(textR66455_12.Text);
                        D_lum[24] = Convert.ToDouble(textR66455_10.Text);
                        D_lum[25] = Convert.ToDouble(textR66455_8.Text);
                        D_lum[26] = Convert.ToDouble(textR66455_6.Text);
                        D_lum[27] = Convert.ToDouble(textR66455_4.Text);
                        D_lum[28] = Convert.ToDouble(textR66455_3.Text);
                        D_lum[29] = Convert.ToDouble(textR66455_2.Text);   //VVV
                        D_x = Convert.ToDouble(txt_R66455_x.Text);
                        D_y = Convert.ToDouble(txt_R66455_y.Text);

                        CaSDKObj.YUV_to_RGB(D_y, D_x, out Lr_d, out Lb_d, out Lg_d);

                        for (int i = 0; i < D_lum.Length; i++)   //VVV
                        {
                            D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                            D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                            D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
                        }
                    }
                    break;
            }
            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(txt_R66455_loff.Text);
                        x_off = Convert.ToDouble(txt_R66455_xoff.Text);
                        y_off = Convert.ToDouble(txt_R66455_yoff.Text);
                        if (r6645x_cbo_index.SelectedIndex == 0)//R66451
                        {
                            if (rBt_R66455_normal5_gamma.Checked == true)
                            {
                                R66451_btnPrevPic_Run("A" + Point_R6645x[12].ToString() + Point_R6645x[12].ToString() + Point_R6645x[12].ToString() + ".dsc_0001.bmp");
                            }
                            else
                            {
                                R66451_btnPrevPic_Run("P" + Point_R6645x[12].ToString() + Point_R6645x[12].ToString() + Point_R6645x[12].ToString() + ".dsc_0001.bmp");
                            }
                        }
                        else //r66455&r66456
                        {
                            if (rBt_R66455_normal5_gamma.Checked == true)
                            {
                                //R66451_btnPrevPic_Run("A" + Point_R6645x[12].ToString() + Point_R6645x[12].ToString() + Point_R6645x[12].ToString() + ".bmp");
                                frmHelp.numPatternR.Value = 255;
                                frmHelp.numPatternG.Value = 255;
                                frmHelp.numPatternB.Value = 255;
                                frmHelp.XX0.Value = (int)(frmHelp.HACT.Value) * 20 / 100; frmHelp.XX1.Value = (int)(frmHelp.HACT.Value) * 80 / 100;//Partial display
                                frmHelp.YY0.Value = (int)(frmHelp.VACT.Value) * 35 / 100; frmHelp.YY1.Value = (int)(frmHelp.VACT.Value) * 65 / 100;
                                Application.DoEvents();
                                frmHelp.OLED_Show_Click(frmHelp.btnlogic_pic, null);
                            }
                            else
                            {
                                Application.DoEvents();
                                frmHelp.OLED_Show_Click(frmHelp.btnwhite, e);
                            }
                        };
                        T20.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }

        private void R6645x_Gamma_Load(object sender, EventArgs e)
        {

        }

        private void T20_Tick(object sender, EventArgs e)
        {
            R6645x_auto_gamma(null, null);
        }

        private void 配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + "R66456" + ".txt";
            if (!File.Exists(path))
            {
                FileInfo myfile = new FileInfo(path);
                FileStream fs = myfile.Create();
                fs.Close();
                StreamWriter sw = File.AppendText(path);
                sw.WriteLine("TUNING_LXY_SPEC(GAMMA0,430,0.300,0.315,2.2)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(0,0.001,0.001,0.15)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(1,0.001,0.001,0.15)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(2,0.001,0.001,0.15)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(3,0.001,0.001,0.15)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(4,0.001,0.001,0.15)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(5,0.001,0.001,0.15)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(6,0.001,0.001,0.15)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(7,0.001,0.001,0.25)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(8,0.001,0.001,0.25)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(9,0.001,0.001,0.25)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(10,0.001,0.001,0.35)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(11,0.001,0.001,0.35)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(12,0.001,0.001,0.45)\r\n" +
                                "F_SET_TUNING_LXY_SPEC(13,0.001,0.001,0.55)\r\n"
                                );
                sw.Flush();
                fs.Close();
            }
            System.Diagnostics.Process.Start(path);
        }

        private void r6645x_cbo_index_SelectedIndexChanged(object sender, EventArgs e)
        {//历史原因，控件名未按数字大小排列，以如下顺序排列： CX为预留Gamma组
            if (r6645x_cbo_index.SelectedIndex == 0) //R66451
            {
                rBt_R66455_HBM_gamma.Text = "nor1_CB";
                rBt_R66455_normal_gamma.Text = "nor1_CA";
                rBt_R66455_normal2_gamma.Text = "nor1_C9";
                rBt_R66455_normal3_gamma.Text = "nor1_C8";
                rBt_R66455_normal6_gamma.Text = "nor1_CX";
                rBt_R66455_normal4_gamma.Text = "nor1_C7";
                rBt_R66455_normal7_gamma.Text = "nor1_CX";
                rBt_R66455_normal5_gamma.Text = "nor1_C6"; //aod
            }
            else if (r6645x_cbo_index.SelectedIndex == 1) //R66455
            {
                rBt_R66455_HBM_gamma.Text = "nor1_CC";
                rBt_R66455_normal_gamma.Text = "nor1_CB";
                rBt_R66455_normal2_gamma.Text = "nor1_CA";
                rBt_R66455_normal3_gamma.Text = "nor1_C9";
                rBt_R66455_normal6_gamma.Text = "nor1_C8";
                rBt_R66455_normal4_gamma.Text = "nor1_CX";
                rBt_R66455_normal7_gamma.Text = "nor1_CX";
                rBt_R66455_normal5_gamma.Text = "nor1_C7";
            }
            else if (r6645x_cbo_index.SelectedIndex == 2) //R66456
            {
                rBt_R66455_HBM_gamma.Text = "nor1_C9";
                rBt_R66455_normal_gamma.Text = "nor1_C8";
                rBt_R66455_normal2_gamma.Text = "nor1_C7";
                rBt_R66455_normal3_gamma.Text = "nor1_C6";
                rBt_R66455_normal6_gamma.Text = "nor1_C5";
                rBt_R66455_normal4_gamma.Text = "nor1_C4";
                rBt_R66455_normal7_gamma.Text = "nor1_C3";
                rBt_R66455_normal5_gamma.Text = "nor1_C0";
            }
        }

        private void R66455_trackBar_Scroll(object sender, EventArgs e)
        {
            R66455_51RegX.Text = R66455_trackBar.Value.ToString() + "阶";
            R66455_txtRegData.Text = "51" + "," + R66455_trackBar.Value.ToString("x4").Substring(0, 2) + "," + R66455_trackBar.Value.ToString("x4").Substring(2, 2);
            R66455_GammaW_Click(R66455_RegW, e);
        }

        private void btn_R66455_Stop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            //--------reset---------------
            Gamma_adjust_timeout_flag = false;
            frmHelp.Gamma_auto_adjust_count = 0;
            //----------------------------
        }

        public void R66451_btnPrevPic_Run(string filenames)       //R66451
        {
            Int32 HACT_True = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            byte[] TemFileContent = new byte[1024 * 100];
            System.Int32 ByteReaded = 0x00;
            try
            {
                //string FileName = textBox2.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString();
                string FileName = System.Windows.Forms.Application.StartupPath + "\\Resources\\vesa\\" + filenames;
                FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                ByteReaded = aFile.Read(TemFileContent, 0, 54);
                HACT_True = (Convert.ToInt32(TemFileContent[19]) << 8) + TemFileContent[18];
                VACT_True = Convert.ToInt32((TemFileContent[23] << 8) + TemFileContent[22]);
                j = 1;
                //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                {
                    aFile.Seek(54, SeekOrigin.Begin);
                    while (j <= VACT_True)
                    {
                        if (HACT_True % 4 != 0)
                            aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                        else
                            aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                        ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                        try
                        {
                            frmHelp.comPort.Write(TemFileContent, 0, ByteReaded);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            break;
                        }
                        m_txlen += ByteReaded;
                        frmHelp.progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        frmHelp.label14.Text = frmHelp.progressBar1.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        j++;
                    }

                    TemFileContent[0] = 0x5A;
                    try
                    {
                        frmHelp.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }

                    aFile.Close();
                }
                /*
                else
                {
                    MessageBox.Show("BMP分辨率非法");
                }*/
            }
            catch
            {
                MessageBox.Show("请选择文件夹", "错误");
            }
        }

        //------------------------------------------R66455_END------------------------------------------

    }
}
